<<Effective OC>>读书笔记 --- 第六条 理解“属性”这一概念
2016-11-07 12:04
501 查看
OC代码
@interface Person : NSObject @property(nonatomic)NSInteger age; // @proerty(nonatimic) NSString name; @end @implementation Person @end
使用property优势:
1、编译器自动合成 存取方法,2、版本兼容,当OC对象新加入一个property,不用编译链接。
因为property早已经计算好偏移量,而直接使用变量则没有这样子的效果,需要重新编译链接才能计算出偏移量。
synthesize作用:目前XCODE7 都不会这么写
@property 声明一个setter getter 方法@synthesize 实现setter getter 方法
那么我想只要一个getter 或者 setter方法怎么办呢?
使用@dynamic
@interface Person : NSObject{ NSString* _name; } @property(nonatomic) NSInteger age; @property(nonatomic) NSString name; @end @implementation Person @dynamic name; //- (void) setName:(NSString*) t //{ // _name = t; //} - (NSString*) name { return _name; } // 省略一万行代码... // 这里会报错,unrecognized selector sent to instance 0x7b870750' // 找不到对应的SEL self.name = @"顶顶顶顶顶"; @end
属性的内存管理语义
strong / weakassign / copy
相关文章推荐
- <<Effective OC>>读书笔记 --- 第八条 理解“对象等同”这一概念
- <<Effective OC>>读书笔记 --- 第十二条 理解消息转发
- <<Effective OC>>读书笔记 --- 第二十四条 将类的实现分散在不同文件中,便于管理
- <<Effective C++>>读书笔记7: 模板与泛型编程
- <<More Effective C++>>读书笔记2: 运算符
- <<Effective c++>>读书笔记---条款20:宁以pass-by-reference-to-const替换pass-by-value
- <<Effective C++>>读书笔记8: 定制new和delete
- <<More Effective C++>>读书笔记6: 杂项
- <<More Effective C++>>读书笔记3: 异常
- <<Effective C++>>读书笔记(三)
- <<Effective C++>>读书笔记
- <<Effective C++>>读书笔记(1)
- <<Effective C++>>读书笔记4: 设计与声明
- <<Effective C++>>读书笔记9: 杂项讨论
- <<Effective C++>>读书笔记(二)
- <<Effective C++>>读书笔记(一)
- <<Effective C++>>读书笔记3: 资源管理
- <<Effective c++>>读书笔记---条款19:设计class犹如设计type
- <<More Effective C++>>读书笔记4: 效率
- <<Information Storage and Management>>读书笔记 之四