-1

is it possible to override a method as a category method ?

@implementation CCSprite (Utilities)

-(void)draw //its already a member of CCSprite but I wantto override it. but within a category
{
   [super draw];
   [self doSomethingExtra];
}
@end

purpose: preventing to copy-paste same codeblock for each drived CCMySprite class.thinking Just import and use.

Fruity Geek
  • 7,351
  • 1
  • 32
  • 41
Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47
  • 1
    You can create subclass of `CCSprite`, let's say `BetterSprite` and then all your derived sprite classes can inherit from `BetterSprite` instead of `CCSprite`. – Tricertops Feb 07 '13 at 22:39
  • Try adding `-(void)dealloc` category method to NSObject and see how bad your app leaks :D – Jeremy Feb 07 '13 at 22:47

1 Answers1

4

No, this isn't suggested by Apple, it's undefined behavior if you do that. One of the two methods will be used in this case, and you can't know which one.

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.

Community
  • 1
  • 1
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187