Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if I accidentally override Apple's private APIs?

Say that Apple has an API defined in a private header file:

// Can't see this at all
@interface NSThing
- (void)secretMethod;
@end

and I have a category:

@interface NSThing (Helpers)
- (void)secretMethod;
@end

Does this override Apple's implementation and will their other private methods start calling into my implementation?

like image 673
ide Avatar asked Feb 14 '14 20:02

ide


1 Answers

From "Avoid Category Method Name Clashes":

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 if you "accidentally" implement a category method with the same name as an existing method (private or not), the behaviour is undefined.

You should therefore prefix your category methods with a prefix that makes name clashes unlikely.

like image 110
Martin R Avatar answered Sep 30 '22 15:09

Martin R