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.基本数据类型:atomic    readwrite    assign
2.普通OC对象:   atomic    readwrite    strong

二、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中移除该对象也不会导致对象被释放。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值