- Instance variable的可见性。
- Objective-C - Private vs Protected vs Public
- Protected methods in Objective-C
- Simulating protected properties and selectors in Objective-C
We can use
@property in Extension the same way as .h file of the class. Compiler can generate the setter and getter methods for us. But in Category, compiler will not generate the setter and getter methods for us, you need to implement them manually or by using
@dynamic to tell the compiler that the setter and getter methods are implemented somewhere else.
The difference comes from the fact that instance variables can be added into Extension but not Category.
to define whether a category can use the extended class’s property or method, just define whether the property or method is in one “interface” that the category can “see”.
protected) does not affect methods. methods are as good as public when visible to clients (and potential pitfalls/bugs when invisible to clients). instead, visibility affects instance variables.
The same priciple as Category: to define whether I can use a property or method (in super class), just define whether the property or method is in an “@interface” the context (where you are trying to use the property or method) can “see”.
performSelector: can run method that are not ready in compile time but ready in runtime. However there will be one warning from the compiler. You can dismiss the warning by declaring a method with the same name in the class in which you are coding, but will introduce another warning saying that “Method definition for *** is not found”.
来自Protected methods in Objective-C的protected method的实现：
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26