简体   繁体   English

强弱IBOutlets之间的区别

[英]Difference between Strong and Weak IBOutlets

What is the difference between strong and weak IBOutlets in the Xcode iOS 5.1 SDK? 是什么区别strongweak在Xcode的iOS 5.1 SDK IBOutlets?

I was previously using the 4.3 SDK, where strong IBOutlets did not exist. 我之前使用的是4.3 SDK,其中没有强大的IBOutlets。 In addition, (auto)release is not available in the iOS 5.1 SDK. 此外,iOS 5.1 SDK中不提供(自动)版本。

Strong means that as long as this property points to an object, that object will not be automatically released. Strong意味着只要此属性指向一个对象,该对象就不会自动释放。 In non-ARC it's a synonym for retain 在非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. 相反, Weak意味着属性指向的对象可以自由释放,但前提是它将属性设置为NULL。 In ARC you use weak to ensure you do not own the object it points to 在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. Nonatomic意味着如果多个线程尝试一次读取或更改属性,则可能发生错误。 Consequences are that there will be partially-written values or over-released objects = CRASH. 后果是会有部分写入的值或过度释放的对象= CRASH。

Take also a look here, at Apple's documents . 请看Apple的文档

From there, examples are 从那里,例子是

@property (weak) IBOutlet MyView *viewContainerSubview;
@property (strong) IBOutlet MyOtherClass *topLevelObject;

Check also this to know more about strong and weak . 检查也知道更多关于strongweak

In ARC (Automatic Reference Counting) Strong tells the compiler that the property-owner relationship is "strong". 在ARC(自动引用计数)中, Strong告诉编译器属性 - 所有者关系是“强”。 It is equivalent to retain in the autorelease pool memory scheme. 它相当于retain在自动释放池内存方案中。 Apple has a article on transitioning to ARC here . Apple在这里有一篇关于过渡到ARC的文章。

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. 您声明为strong的属性,它拥有该对象,编译器将负责任何对象分配此属性。 This property will destroyed when we set to nil. 当我们设置为nil时,此属性将被销毁。

When you don't want the control life time then u declare as a week property. 如果您不想要控制生命周期,那么您将声明为周属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM