Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding methods using categories in Objective-C

Can I use a class category to override a method that is already implemented using a category? Like this:

1) Original method

-(BOOL) method {   return true; } 

2) Overrided method

-(BOOL) method {   NSLog(@"error?");    return true;  } 

Will this work, or is this illegal?

like image 792
retix Avatar asked Mar 11 '11 11:03

retix


People also ask

Does Objective-C support method overriding?

Correct, objective-C does not support method overloading, so you have to use different method names.

What is the use of category in Objective-C?

Categories provide the ability to add functionality to an object without subclassing or changing the actual object. A handy tool, they are often used to add methods to existing classes, such as NSString or your own custom objects.

How do you select a method to override?

quick command search : ctrl + shift + A Type constructor to override all of them quickly :D.

Can we override method in extension?

Extensions cannot/should not override. It is not possible to override functionality (like properties or methods) in extensions as documented in Apple's Swift Guide. Extensions can add new functionality to a type, but they cannot override existing functionality.


2 Answers

From Apple documentation:

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.

like image 163
Benoît Avatar answered Sep 23 '22 03:09

Benoît


You can do this by adapting Class Cluster approach, or using methods swizzling technique.

Otherwise, the behavior of two or more categorized methods is undefined

like image 33
Martin Babacaev Avatar answered Sep 19 '22 03:09

Martin Babacaev