ARC初探

来源:互联网 发布:网络优化工程师 西安 编辑:程序博客网 时间:2024/06/06 09:06

1.atomic// default


2.nonatomic


3.strong(ios4 = retain) // default

> Keep this in heap until I don’t point it any more.

> Used when you need to retain the object.

> ARC automatically releases it for you when you are done with it.

 

4.weak

> Keep this as long as someone else points it strongly.

> The same thing as assign, no retain norrelease. So weak is essentially anassign, unretainedproperty, except that the weak pointer is auto set to nil when the object is deallocated. 

> Use weak for IBOutlets(UIViewController’s children). The child objectsonly need to exist as long as parent object does.

> Used to avoid retain cycle

 

5.retain

> The same as strong in property declaration.

 

6.assign// default

> Simply perform a variable assignment. Tell the compiler how to synthesizethe setter implementation.

> Assign for C primitive properties and weakfor weak references to Objective-C objects.

 

7.unsafe_unretained(in iOS4 and below, it is the same as assign)

> The new pointer is stored into the lvalue using the primitive semantics.

> One day, you could search for unsafe_unretained and replace them with weak. This is easier than searchingfor assign and figure out if you actually meant assign or weak.

> “unsafe_unretained”would be left as a dangling pointer, which is unsafe.Sending message to a dangling pointer will result in a crash.

 

8.copy

> Required when the object is mutable, andyou need the value at the moment.

> copy一个对象并且为其创建一个strong指针。

 

9.readonly

10.readwrite // default

总结:

1. property也可以用 strong 或 weak 来标记,简单地把原来写 retain和 assign 的地方替换成 strong和 weak 就可以了(ARC下可以统一使用strong、weak,基本类型仍然使用assign,如int、BOOL)。


2. ARC确实可以在适当的地方为代码添加 retain 或者 release ,但是这并不意味着你可以完全忘记内存管理,因为你必须在合适的地方把 strong指针手动设置到nil,否则app很可能会oom。简单说还是那句话,你必须时刻清醒谁持有了哪些对象,而这些持有者在什么时候应该变为指向 nil 。

 

0 0
原创粉丝点击