1. extension is more like Anonymous category (class extensions are like Anonymous categories) 2. Category is more inclined to be written in an independent file, and then named "nsview + customadditions. h ", in the corresponding. block in the M file. Therefore, cagegory tends to be used to separate functions of classes and organize functional modules of classes. Extension is more inclined to write at the top of the. M file of the same class, and its implementation is placed in the implementation block of the class. Through this sentence, we feel that the private function for implementing the objc class adopts the class
Extension
3. Variables cannot be defined in category. Extension can define variables and properties as follows:
@interface MyClass : NSObject |
- (float)value; |
@end |
@interface MyClass () { |
float value; |
} |
- (void)setValue:(float)newValue; |
@end |
@implementation MyClass |
- (float)value { |
return value; |
} |
- (void)setValue:(float)newValue { |
value = newValue; |
} |
@end |