您的位置:首页 > 其它

Class Extensions -匿名类别

2013-10-13 11:44 381 查看
.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对外是只读的,文件内又可以修改。}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: