.Class Extensions
跟类别的行为很像,被成为匿名类别。跟类别不同的时,Extension里面可以存放实体变量,而且在Extension内实现的方法,在implementation一定要实现。
基本形式:
@interface XYZPerson ()
@property NSObject *extraProperty;
@end
Extension 主要用于 to Hide Private Information,定义在Extension内的变量,外部文件无法访问。另外在Extension内,可以重新声明变量的属性,比如一个变量对外设置为只读,对内可读可写:
@interface XYZPerson : NSObject
@property (readonly) NSString *uniqueIdentifier;
@end
@interface XYZPerson ()
@property (readwrite) NSString *uniqueIdentifier;
@end
@implementation XYZPerson
...
@end
这样,就实现了uniqueIdentifier对外是只读的,文件内又可以修改。}