Setter Semantics即@property中使用的修饰符
Setter Semantics
These attributes specify the semantics of a set accessor. They are mutually exclusive.
strong
Specifies that there is a strong (owning) relationship to the destination object.
weak
Specifies that there is a weak (non-owning) relationship to the destination object.
If the destination object is deallocated, the property value is automatically set to nil.
(Weak properties are not supported on OS X v10.6 and iOS 4;
use assigninstead.)
copy
Specifies that a copy of the object should be used for assignment.
The previous value is sent a release message.
The copy is made by invoking the copy method. This attribute is valid only for object types, which must implement the NSCopying protocol.
assign
Specifies that the setter uses simple assignment. This attribute is the default.
You use this attribute for scalar types such as NSInteger and CGRect.
retain
Specifies that retain should be invoked on the object upon assignment.
The previous value is sent a release message.
In OS X v10.6 and later, you can use the __attribute__ keyword to specify that a Core Foundation property should be treated like an Objective-C object for memory management:
@property(retain) __attribute__((NSObject)) CFDictionaryRef myDictionary;
一、关于ARC下,不显示指定属性关键字时,默认关键字有哪些?
1.基本数据类型:atomicreadwriteassign
2.普通OC对象:atomicreadwritestrong
二、strong,weak, unsafe_unretained往往都是用来声明属性的,如果想声明临时变量就得用__strong,__weak, __unsafe_unretained。
一般的临时指针变量默认就是strong类型的,因此一般我们对于strong变量不加__strong修饰,以下两行代码是等价的:
NSString *name = self.nameField.text;
__strong NSString *name = self.nameField.text;
三、以下代码在ARC之前是可能会行不通的,因为在手动内存管理中,从NSArray中移除一个对象时,这个对象会发送一条release消息,可能会被立即释放。随后NSLog()打印该对象就会导致应用崩溃。
id obj = [array objectAtIndex:0];
[array removeObjectAtIndex:0];
NSLog(@"%@", obj);
在ARC中这段代码是完全合法的,因为obj变量是一个strong指针,它成为了对象的拥有者,从NSArray中移除该对象也不会导致对象被释放。
【Setter Semantics即@property中使用的修饰符】
推荐阅读
- 即将到手三百万
- 离开美即
- 夏天了,来一首入耳即化的音乐吧
- 2020年,财富逻辑的大变迁
- 中国军校
- 颠覆世界|下弦集|为了你们,我要走遍全世界_笔若的诗?
- K8S|K8S 生态周报| Istio 即将发布重大安全更新,多个版本受影响
- 《绿皮书》(即使白人在背书,也无法做到皆大欢喜)
- 李敏镐今日退伍,帅气欧巴即将回归
- 碎碎念|碎碎念||即将启程的下一段人生旅程