iphone - 为什么我应该更喜欢 __unsafe_unretained 限定符而不是为弱引用属性分配?

标签 iphone ios memory-management automatic-ref-counting

<分区>

Possible Duplicate:
using ARC, lifetime qualifier assign and unsafe_unretained

两者有什么区别?

@property(unsafe_unretained) MyClass *delegate;
@property(assign) MyClass *delegate;

两者都是非归零弱引用,对吧?那么有什么理由让我写更长更难读的 unsafe_unretained 而不是 assign

注意:我知道 weak 是一个归零引用。但它只是 iOS >= 5。

最佳答案

在属性访问器中,是的,它们是相同的。在这种情况下,assign 属性将映射到 unsafe_unretained。但请考虑手动编写 ivar 而不是使用 ivar 合成。

@interface Foo
{
   Bar *test;
}
@property(assign) Bar *test;
@end

此代码现在在 ARC 下是不正确的,而在 ARC 之前它不是。所有 Obj-C 对象的默认属性是 __strong 向前发展。向前推进的正确方法如下。

@interface Foo
{
   __unsafe_unretained Bar *test;
}
@property(unsafe_unretained) Bar *test;
@end

或者使用ivar合成只是@property(unsafe_unretained) Bar *test

所以实际上它只是写同一件事的不同方式,但它显示了不同的意图。

关于iphone - 为什么我应该更喜欢 __unsafe_unretained 限定符而不是为弱引用属性分配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8806831/

相关文章:

iphone - 如何使用 SA_OAuthTwitterEngine 在没有 PIN 的情况下从 iPhone 发布推文?

iphone - UIImage imageWithData 需要一些时间才能出现

c - 带 void 的动态数组内存分配**

python - 如何在 Windows 上使用 64 位 Python 调试(可能与 C 库相关的)内存问题?

c++ - 如何停止线程并将其寄存器刷新到堆栈中?

即使我正在释放,iPhone 应用程序也会由于收到内存警告而崩溃

iphone - 使用叠加 View 制作 MPMoviePlayer 的更好方法

iphone - 设置状态栏隐藏后不旋转

iOS8 UISearchController 掩盖了 UITableView

iphone - 通过套接字发送图像数据,但它在到达服务器时接收到一些不同的格式