xcode 强IBOutlets与弱IBOutlets的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11168992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Strong and Weak IBOutlets
提问by Dinesh
What is the difference between strong
and weak
IBOutlets in the Xcode iOS 5.1 SDK?
strong
和weak
Xcode iOS 5.1 SDK 中的 IBOutlets 有什么区别?
I was previously using the 4.3 SDK, where strong IBOutlets did not exist. In addition, (auto)release is not available in the iOS 5.1 SDK.
我之前使用的是 4.3 SDK,其中不存在强大的 IBOutlets。此外,(自动)发布在 iOS 5.1 SDK 中不可用。
回答by Phillip
Strong
means that as long as this property points to an object, that object will not be automatically released. In non-ARC it's a synonym for retain
Strong
意味着只要这个属性指向一个对象,那个对象就不会被自动释放。在非 ARC 中,它是retain
Specifies that there is a strong (owning) relationship to the destination object.
指定与目标对象存在强(拥有)关系。
Weak
instead, means that the object the property points to, is free to release but only if it sets the property to NULL. In ARC you use weak to ensure you do not own the object it points to
Weak
相反,意味着该属性指向的对象可以自由释放,但前提是它将该属性设置为 NULL。在 ARC 中,您使用 weak 来确保您不拥有它指向的对象
Specifies that there is a weak (non-owning) relationship to the destination object. If the destination object is deallocated, the property value is automatically set to nil.
指定与目标对象之间存在弱(非拥有)关系。如果目标对象被释放,属性值会自动设置为 nil。
Nonatomic
means that if multiple threads try to read or to change the property at once, badness can happen. Consequences are that there will be partially-written values or over-released objects = CRASH.
Nonatomic
意味着如果多个线程尝试同时读取或更改属性,则可能会发生错误。结果是会有部分写入的值或过度释放的对象 = CRASH。
Take also a look here, at Apple's documents.
也看看这里,在苹果的文档。
From there, examples are
从那里,例子是
@property (weak) IBOutlet MyView *viewContainerSubview;
@property (strong) IBOutlet MyOtherClass *topLevelObject;
Check also thisto know more about strong
and weak
.
也检查这个以了解更多关于strong
和weak
。
回答by doggiedoc
In ARC (Automatic Reference Counting) Strong
tells the compiler that the property-owner relationship is "strong". It is equivalent to retain
in the autorelease pool memory scheme. Apple has a article on transitioning to ARC here.
在 ARC(自动引用计数)中,Strong
告诉编译器财产所有者关系是“强”的。它相当于retain
在自动释放池内存方案中。Apple 在此处有一篇关于过渡到 ARC 的文章。
回答by Ganesh
The property which you declare as a strong , it owns the object and the compiler will take care that which any object assigns this property. This property will destroyed when we set to nil.
您声明为 strong 的属性,它拥有该对象,编译器将负责任何对象分配此属性的内容。当我们设置为 nil 时,这个属性将被销毁。
When you don't want the control life time then u declare as a week property.
当您不想要控件生命周期时,您可以将其声明为周属性。