ARC - __unsafe_unretained的含义是什么? [英] ARC - The meaning of __unsafe_unretained?

查看:99
本文介绍了ARC - __unsafe_unretained的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想确保我做对了:


  1. 我需要 __ unsafe_unretain 我不拥有的对象?

  2. 如果对象是 __ unsafe_unretained 我是否需要使用 @property 中分配?这是否意味着该对象未被保留,只是引用我分配给的对象?

  3. 除了委托之外我何时才想使用它?

  4. 这是ARC的事情还是以前用过的?

  1. Do I need to __unsafe_unretain objects that I don't own?
  2. If an object is __unsafe_unretained Do I need to use assign in the @property? Does that mean that the object is not retained, and just refers to the object I assign to?
  3. When would I want to use it except of delegates?
  4. Is that an ARC thing or was it in use before?


推荐答案

LLVM编译器3.0引入了四个新的所有权限定符: __ strong __ autoreleasing __ unsafe_unretained __弱。根据规范,即使在ARC之外,前三个也可用。

The LLVM Compiler 3.0 introduces four new ownership qualifiers: __strong, __autoreleasing, __unsafe_unretained, and __weak. The first three are available even outside ARC, as per the specification.

正如Joshua所指出的那样,默认情况下,所有指针都暗示为ARC下的 __ strong 。这意味着当一个对象被分配给该指针时,只要该指针引用它,它就会被保留。对于大多数事情来说这很好,但它为保留周期开辟了可能性,正如我在答案此处中所述。例如,如果您有一个包含另一个对象作为实例变量的对象,但该第二个对象具有一个强大的链接作为其委托的第一个对象,那么这两个对象将永远不会被释放。

As Joshua indicates, by default all pointers are implied to be __strong under ARC. This means that when an object is assigned to that pointer, it is retained for as long as that pointer refers to it. This is fine for most things, but it opens up the possibility for retain cycles, as I describe in my answer here. For example, if you have an object that contains another object as an instance variable, but that second object has a strong link back to the first one as its delegate, the two objects will never be released.

由于这个原因,存在 __ unsafe_unretained __弱限定符。它们最常见的用途是代表,您可以使用 unsafe_unretained 属性为该代理定义属性( assign 实际上是 unsafe_unretained ),然后通过用标记相应的实例变量来匹配__weak __ unsafe_unretained 。这意味着委托实例变量仍将指向第一个对象,但它不会导致保留该对象,从而打破保留周期并允许释放两个对象。

It is for this reason that the __unsafe_unretained and __weak qualifiers exist. Their most common use is for delegates, where you'd define a property for that delegate with the weak or unsafe_unretained attribute (assign is effectively unsafe_unretained), and then match that by marking the respective instance variable with __weak or __unsafe_unretained. This means that the delegate instance variable will still point back at the first object, but it will not cause that object to be retained, thus breaking the retain cycle and allowing both objects to be released.

除了代理之外,这对于破坏代码中可能形成的任何其他保留周期非常有用。有用的是,Leaks仪器现在包含一个Cycles视图,它以图形方式显示它在应用程序中发现的保留周期。

Beyond delegates, this is useful to break any other retain cycles that might form in your code. Helpfully, the Leaks instrument now includes a Cycles view, which shows retain cycles it discovers in your application in a graphical manner.

两者 __ unsafe_unretained __ weak 阻止保留对象,但方式略有不同。对于 __ weak ,指向对象的指针将在它指向的对象的重新分配时转换为 nil ,这是非常安全的行为。顾名思义, __ unsafe_unretained 将继续指向对象所在的内存,即使它已被解除分配。这可能会因访问已解除分配的对象而导致崩溃。

Both __unsafe_unretained and __weak prevent the retention of objects, but in slightly different ways. For __weak, the pointer to an object will convert to nil on the deallocation of the object it points to, which is very safe behavior. As its name implies, __unsafe_unretained will continue pointing to the memory where an object was, even after it was deallocated. This can lead to crashes due to accessing that deallocated object.

为什么要使用 __ unsafe_unretained 呢?不幸的是, __ weak 仅支持iOS 5.0和Lion作为部署目标。如果你想回到iOS 4.0和Snow Leopard,你必须使用 __ unsafe_unretained 限定符,或使用Mike Ash的 MAZeroingWeakRef

Why would you ever use __unsafe_unretained then? Unfortunately, __weak is only supported for iOS 5.0 and Lion as deployment targets. If you want to target back to iOS 4.0 and Snow Leopard, you have to use the __unsafe_unretained qualifier, or use something like Mike Ash's MAZeroingWeakRef.

这篇关于ARC - __unsafe_unretained的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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