1、extension更像是匿名的category(
Class extensions are like anonymous categories)
2、category更倾向于写在独立的文件中,之后这样命名“
NSView+CustomAdditions.h”,在对应的.m文件中的block块中实现。所以,cagegory更倾向于用于对class进行功能的分离,用于组织类的功能模块。
extension更倾向于在同一个类的.m文件的最上方写,而其实现则放在该类的implementation block中。通过这句话,感觉实现Objc类的私有函数,采用的是class extension
3、在category中不可以定义变量,extension可以定义变量和property
如下面的例子:
@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 |