ios property 之 strong weak unsafe_unretained

同样的话题再重申一遍:

1 自己无法解决的问题是指:一段时间内找不到答案的问题都是自己无法解决的问题(不要在不会的问题上耗太长时间)

2 不懂就要问题,问百度,问leader,还是问自己,问官网,问技术群,权衡好。

3 英文不好,可以先搜中文,中文无法解决来英文,或者直接上官网找答案。

4 分享应该作为一种乐趣,不仅益于他人,更能提高自己,好记星不如烂笔头,记录下来。

一下内容源于apple开发网

【https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW2】

strong(一定要避免:Strong Reference Cycles)

//When one object relies on other objects in this way, effectively taking ownership of those other objects, the first object is said to have strong references to the other objects. 
//In Objective-C, an object is kept alive as long as it has at least one strong reference to it from another object. 
//There’s no need to specify the strong attribute explicitly, because it is the default.
@property id delegate;

言而简之:1 strong会使右对象引用计数加1,strong是默认属性,即:不指明的话,默认就是strong。2 Local variables (and non-property instance variables) also maintain strong references to objects by default.3 A variable maintains a strong reference to an object only as long as that variable is in scope, or until it is reassigned to another object or nil.也就是说:当nil一个对象时,当前对象就会减少一次对其所执行对象的引用技术。

weak

//The opposite to weak is strong. There’s no need to specify the strong attribute explicitly, because it is the default.

@property (weak) id delegate;

Because a weak reference doesn’t keep an object alive, it’s possible for the referenced object to be deallocated while the reference is still in use. To avoid a dangerous dangling pointer to the memory originally occupied by the now deallocated object, a weak reference is automatically set to nil when its object is deallocated.

unsafe_unretained

There are a few classes in Cocoa and Cocoa Touch that don’t yet support weak references, which means you can’t declare a weak property or weak local variable to keep track of them. These classes include NSTextViewNSFont and NSColorSpace; for the full list, see Transitioning to ARC Release Notes.

If you need to use a weak reference to one of these classes, you must use an unsafe reference. For a property, this means using the unsafe_unretained attribute:

@property (unsafe_unretained) NSObject *unsafeProperty;

For variables, you need to use __unsafe_unretained:

    NSObject * __unsafe_unretained unsafeReference;

An unsafe reference is similar to a weak reference in that it doesn’t keep its related object alive, but it won’t be set to nil if the destination object is deallocated. This means that you’ll be left with a dangling pointer to the memory originally occupied by the now deallocated object, hence the term “unsafe.” Sending a message to a dangling pointer will result in a crash.

附加代码样例

NSObject * __weak someObject = [[NSObject alloc] init];

Note: The opposite to __weak is __strong. Again, you don’t need to specify __strong explicitly, because it is the default.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值