Overriding methods using categories in Objective-C

后端 未结 4 697
悲哀的现实
悲哀的现实 2020-11-22 04:37

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

1) Original method

-(BOOL) method {
  retur         


        
4条回答
  •  长情又很酷
    2020-11-22 05:14

    Old documentation link is dead; best replacement I could find was here: Apple Docs:

    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.

    It's Apple using a lighter touch, but the main point is the same: you invite disaster, because the unpredictable behavior is silent.

提交回复
热议问题