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

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

问题描述

只是想确保我做对了:

  1. 我是否需要__unsafe_unretain 我不拥有的对象?
  2. 如果对象是__unsafe_unretained 我是否需要在@property 中使用assign?那是不是表示对象没有被保留,只引用我分配给的对象?
  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 Compiler 3.0 引入了四个新的所有权限定符:__strong__autoreleasing__unsafe_unretained__weak.根据规范,前三个甚至在 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__weak 限定词存在.它们最常见的用途是用于委托,您可以使用 weakunsafe_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天全站免登陆