Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override a method in Objective c via category

Tags:

The following is working in objective c:

// Base Class in ClassA.h and ClassA.m
@interface ClassA : NSObject 
- (NSString *) myMethod;
@end
@implementation ClassA
- (NSString*) myMethod { return @"A"; }
@end

//Category in ClassA+CategoryB.h and ClassA+CategoryB.m
@interface ClassA (CategoryB) 
- (NSString *) myMethod;
@end
@implementation ClassA (CategoryB)
- (NSString*) myMethod { return @"B"; }
@end

The question is, if I am just importing ClassA.h and send the message

[myClassA myMethod]; //returns B

why is this returning B? I am not importing ClassA+CategoryB

Even futhrer, if I did the following:

// Base Class in ClassA.h and ClassA.m
@interface ClassA : NSObject 
- (NSString *) myMethod;
- (NSString *) mySecondMethod;
@end
@implementation ClassA
- (NSString*) myMethod { return @"A"; }
- (NSString *) mySecondMethod { return [self myMethod]; }
@end

//Category in ClassA+CategoryB.h and ClassA+CategoryB.m
@interface ClassA (CategoryB) 
- (NSString *) myMethod;
@end
@implementation ClassA (CategoryB)
- (NSString*) myMethod { return @"B"; }
@end

and call mySecondMethod:

ClassA *a = [[ClassA alloc] init];
NSLog(@"%@",[a myMethod]);

the result will still be B although nobody knows (due to no import) of the category implementation?!

I'd excepted, only to return Bif I was importing the category...

So any hints appreciated.

like image 984
Alexander Avatar asked Jan 10 '13 13:01

Alexander


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 you override an instance method?

3) An instance method cannot override a static method, and a static method cannot hide an instance method. For example, the following program has two compiler errors.


1 Answers

Objective-C messaging is dynamic, this means that it doesn't matter if you import or not the category. The object will receive the message and execute that method.

The category is overriding your method. This means that when the runtime sends a message to that object, will always find the overridden method, no matter what you import.

If you want to ignore a category you shouldn't compile it, so you could remove the category from the compiler sources.
An alternative is subclassing.

Also read this:

Avoid Category Method Name Clashes

Because the methods declared in a category are added to an existing class, you need to be very careful about method names.

If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime. This is less likely to be an issue if you’re using categories with your own classes, but can cause problems when using categories to add methods to standard Cocoa or Cocoa Touch classes.

So in your case you haven't got any problem because as stated, this is less likely to happen with user defined classes. But you should definitely use subclassing instead of writing a category.

like image 175
Ramy Al Zuhouri Avatar answered Oct 12 '22 10:10

Ramy Al Zuhouri