ARC推出的新型限定符有哪些? [英] What are the new type qualifiers introduced with ARC?

查看:161
本文介绍了ARC推出的新型限定符有哪些?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自动引用计数(ARC)引入了一些新的类型限定符。我见过 __ strong __弱,但是他们做了什么?

Automatic Reference Counting (ARC) introduces some new type qualifiers. I've seen __strong and __weak, but what do they do?

推荐答案

__ strong 表示在赋值时,表达式的rvalue将被保留并使用原始语义存储到左值中。 (要解除分配这样的对象,你必须做的只是分配 nil ,之前引用的对象将被释放, nil 将被保留,实际上什么也没做,它是桃子和奶油。)

__strong means that on assignment, the rvalue of the expression will be retained and stored into the lvalue using primitive semantics. (To deallocate such an object, all you must do is assign it nil, the previously referenced object will be released, nil will be retained, which effectively does nothing and it's peaches and cream.)

__ unsafe_unretained __ weak 在rvalue的地址将分配给左值的意义上是相似的,但是如果你使用 __ weak 限定符,操作保证是原子的并且受到一些不同的语义的影响。其中之一是,如果正在分配的对象当前正在进行解除分配,那么赋值将评估为 nil ,然后将其原子地存储回到左值表达方式。因此措辞 __ unsafe_unretained ,因为该操作确实不安全且没有保留。

__unsafe_unretained and __weak are similar in the sense that the address of the rvalue will be assigned to the lvalue, but if you use the __weak qualifier, this operation is guaranteed to be atomic and subject to some different semantics. One of these are that if the object that is being assigned is currently undergoing deallocation, then the assignment will evaluate to nil and that will then be atomically stored back in to the lvalue of the expression. Hence the wording __unsafe_unretained, because that operation is indeed unsafe and unretained.

__ autoreleasing 类似于 __ strong ,除了它有一个警告:保留的对象被推送到当前的自动释放池,因此您可以获取对象的临时所有权从集合中删除它,然后将其返回给调用者。还有其他用途,但它们主要与获取对象的临时所有权有关。

__autoreleasing is like __strong except it has one caveat: The retained object is pushed onto the current autorelease pool, so you can for example obtain temporary ownership of an object to remove it from a collection and then return it back to the caller. There are other uses for this, but they mostly have to do with obtaining temporary ownership of an object.

这些行为也出现在相应的属性修饰符中( strong unsafe_unretained )。

These behaviors also present themselves in the corresponding property modifiers (strong, unsafe_unretained and weak).

请参阅 Clang自动参考计数技术规范

编辑:对于那些未针对iOS 5而无法获得 __弱 Mike Ash写道关于归零弱引用的精彩文章(和实现),您可以使用它。

For those not targeting iOS 5 and therefore unable to reap the benefits of __weak, Mike Ash wrote a superb article (and implementation) on zeroing weak references which you can use instead.

这篇关于ARC推出的新型限定符有哪些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆