Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Override on Category #58

Open
Fykec opened this issue Dec 20, 2013 · 8 comments
Open

Override on Category #58

Fykec opened this issue Dec 20, 2013 · 8 comments

Comments

@Fykec
Copy link

Fykec commented Dec 20, 2013

Hi Marin

I found the override on category not work on my code base

my subclass's + (NSDictionary *)mappings will not be called if I use ObjetiveRecord by Cocoapods,
and + (NSString *)entityName will return NSManagedObject if this call if from NSManagedObject+Mappings.m

I found some explanation, do you think so?

Obj-C allows you to add methods to an existing class using Categories. So if you add a method to NSString, the categoriesed method is available to NSMutableString and all classes which inherits NSString or any subclasses of NSString.

But you should avoid to Override the category.

You will not be 100% sure which method will be called. It depends on compiler.
http://stackoverflow.com/questions/14259517/override-a-method-in-objective-c-via-category

@stephencelis
Copy link
Collaborator

@Fykec Do you have those same methods already defined on NSManagedObject? Are you using another library that defines these methods?

@Fykec
Copy link
Author

Fykec commented Mar 27, 2014

No, I do not already defined a same method for NSManagedObject, other library also do not have

@stephencelis
Copy link
Collaborator

@Fykec I'm confused as to what the problem is. Could you explain what the expected behavior is and the behavior you're seeing? Perhaps with some code samples?

@Fykec
Copy link
Author

Fykec commented Mar 27, 2014

in ObjectiveRecord lib NSManagedObject have the mappings method, in design You want subclass to override the mappings method, but the mappings method define in category, so here is overrider category's method, I found I cannot override success, so I post the http://stackoverflow.com/questions/14259517/override-a-method-in-objective-c-via-category about this. Now I just want to confirm if have this override failure problem with you.

@supermarin
Copy link
Owner

@Fykec if you're on master, mappings are +mappings instead of previous -mappings.
Try swap them out

@Fykec
Copy link
Author

Fykec commented Mar 27, 2014

Yeah I have try the +mappings and -mappings before, It seem's like both not ok, Now I can not find the original codes to reproduce this situation. And I found Apple do not suggest to override the category,

Although the Objective-C language currently allows you to use a category to override methods the class inherits, or even methods declared in the class interface, you are strongly discouraged from doing so. A category is not a substitute for a subclass. There are several significant shortcomings to using a category to override methods:

When a category overrides an inherited method, the method in the category can, as usual, invoke the inherited implementation via a message to super. However, if a category overrides a method that exists in the category's class, there is no way to invoke the original implementation.

A category cannot reliably override methods declared in another category of the same class.

This issue is of particular significance because many of the Cocoa classes are implemented using categories. A framework-defined method you try to override may itself have been implemented in a category, and so which implementation takes precedence is not defined.

The very presence of some category methods may cause behavior changes across all frameworks. For example, if you override the windowWillClose: delegate method in a category on NSObject, all window delegates in your program then respond using the category method; the behavior of all your instances of NSWindow may change. Categories you add on a framework class may cause mysterious changes in behavior and lead to crashes.

http://stackoverflow.com/questions/5272451/overriding-methods-using-categories-in-objective-c

@supermarin
Copy link
Owner

@Fykec thank you for your explanation.
I believe we're both aware how overriding categories behaves (and misbehaves), and are working on a solution that'll implement this in a sane way.

@PauliusVindzigelskis
Copy link

One of quick solutions are adding prefix for every category method like +orr_mappings. This way You are sure that most of the times there will be no conflicts unless other library uses same prefix

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants