property中的关键字

示例

1 @property (nonatomic, strong)NSString *name;
2 @property (nonatomic, assign)NSInteger age;

1.nonatomic, atomic

atomic 原子操作:给资源进行加锁解锁 ,安全 , 效率不高(如12306买火车票系统)
nonatomic 非原子操作: 没有加锁解锁机制 ,不安全, 效率高

大部分情况使用nonatomic

 2.strong,retain,weak

strong: property关键字,声明对不是自己创建的对象的拥有权 retainCount ++(对象不会被释放掉)

 retain: 除了property关键字之外 在代码里面也需要声明拥有权

 weak : 弱引用 只是定义了某一个类型的对象 不会对这个对象声明拥有权,对象会被外部释放掉

OC的对象类型 就用strong。retain 和 strong 相同,但一般用strong

3.copy

 copy: 拷贝一份 ,不是所有的对象类型都能用, 如果要使用copy ,对象必须服从NSCopying协议 如NSString NSArray Block,自己创建的对象必须服从NSCopying协议才能使用这个关键字

4.readwrite,readonly

readwrite :默认 可读可写 提供了set和get方法

 readonly: 只读 只提供了get方法

1 @property (nonatomic, strong, readwrite)NSString *name;//有get和set方法
2 @property (nonatomic, strong, readonly)NSString *sex;//只有get方法

 5.更改系统提供的setter和getter方法名

setter=funcName:

 getter=

1 @property (nonatomic, strong, setter = setMyName:) NSString *name;
2 @property (nonatomic, strong, getter = getMySex) NSString *sex;

注:如果方法有参数,要在方法名后面加:

6.assign

修饰C语言的基本数据类型

 为了防止循环引用 delegate

assign是弱引用

猜你喜欢

转载自www.cnblogs.com/jianze/p/9379651.html