-
Notifications
You must be signed in to change notification settings - Fork 194
Override on Category #58
Comments
@Fykec Do you have those same methods already defined on NSManagedObject? Are you using another library that defines these methods? |
No, I do not already defined a same method for NSManagedObject, other library also do not have |
@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? |
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. |
@Fykec if you're on master, mappings are |
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 |
@Fykec thank you for your explanation. |
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 |
Hi Marin
I found the override on category not work on my code base
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
The text was updated successfully, but these errors were encountered: