简体   繁体   English

强弱财产有什么区别?

[英]what is the difference between strong and weak property?

As strong and weak properties are new in iOS 5. If any one tell me which property is used when. 由于iOS 5中的强弱属性是新的。如果有人告诉我何时使用了哪个属性。

When we should use strong or when we should use weak? 什么时候应该使用强者还是应该使用弱者?

@property(nonatomic,strong)

@property(nonatomic,weak)

strong is like retain , weak is like assign . strong就像retainweak就像assign The main difference is that weak properties turn to nil when the object that is assigned to them gets released. 主要区别在于,当分配给它们的对象被释放时, weak属性变为nil

eg: 例如:

@property (nonatomic, weak) id test;

...

- (void)example
{
    id foo = [[NSObject alloc] init];
    self.test = foo;
    foo = [[NSObject alloc] init];
    assert(self.test == nil);
}

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的同义词。

Weak instead, means that the object the property points to, is free to release but only if it sets the property to nil . 相反, Weak意味着属性指向的对象可以自由释放,但前提是它将属性设置为nil In ARC you use weak to ensure you do not own the object it points to. 在ARC中,您使用weak来确保您不拥有它指向的对象。

Review Apple documentation for the Automatic Reference Counting (ARC) 查看Apple文档以获取自动参考计数(ARC)

If you don't have time for reading it: 如果您没有时间阅读它:

ARC introduces several new lifetime qualifiers for objects, and weak references. ARC为对象和弱引用引入了几个新的生命周期限定符。 A weak reference does not extend the lifetime of the object it points to, and automatically becomes nil when there are no strong references to the object. 弱引用不会延长它指向的对象的生命周期,并且在没有对该对象的强引用时自动变为nil。

strong is the default. 是默认的。 An object remains “alive” as long as there is a strong pointer to it. 只要存在指向它的强指针,对象就会保持“活着”。

weak specifies a reference that does not keep the referenced object alive. weak指定不保持引用对象存活的引用。 A weak reference is set to nil when there are no strong references to the object. 当没有对象的强引用时,弱引用设置为nil。

As iOS 5 ARC automatically nullifies weak links, when an object is unloaded its object hierarchy is automatically set to nil. 由于iOS 5 ARC自动使弱链接无效,因此在卸载对象时,其对象层次结构将自动设置为nil。 Because this reason, Weak is the recommended relationship for all outlet properties. 由于这个原因, Weak是所有outlet属性的推荐关系。 These view objects are already part of the view controller's view hierarchy and don't need to be retained elsewhere. 这些视图对象已经是视图控制器视图层次结构的一部分,不需要在其他地方保留。 The big advantage of declaring your outlets weak is that it saves you time writing the viewDidUnload method. 声明你的网点很弱的一大优点是它可以节省你编写viewDidUnload方法的时间。

Check a very detailed document refering memory management . 查看一个非常详细的文档,参考内存管理 It is previous to ARC, but it will help you to understand the memory management. 它是ARC之前的版本,但它可以帮助您理解内存管理。 The retain keyword for properties still works with ARC and is simply a synonym for strong. 属性的retain关键字仍然适用于ARC,只是strong的同义词。 Or another specific ARC tutorial . 或者另一个特定的ARC教程

There are following difference between strong and weak. 强弱之间存在以下差异。

1.If we declare variable strong then it is not deallocated by compiler till the Application instance in memory.When we set nil value to that reference then it deallocates by compiler, by default any local variable as strong variable. 1.如果我们声明变量强,那么它不会被编译器解除分配,直到内存中的Application实例。当我们将nil值设置为该引用时,它会被编译器解除分配,默认情况下任何局部变量都是强变量。 For instance:- var str = "hello world" 例如: - var str =“hello world”

if we set str = nil then it is deallocated. 如果我们设置str = nil则将其解除分配。

2.If we declare variable as strong then it is retain by other instance(Class) and it's retain count increment by 1. 2.如果我们将变量声明为强,那么它将被其他实例(Class)保留,并且它将保持计数增量1。

Weak property. 财产薄弱。

1.When we declare weak property then it only contains data/instance address till strong reference is in memory if strong variable reference deallocated it is automatically set to nil. 1.当我们声明弱属性时,它只包含数据/实例地址,直到强引用在内存中,如果强大的变量引用被释放,它将自动设置为nil。

For ex:- var str = "hello world" weak var stringVar = str 例如: - var str =“hello world”weak var stringVar = str

suppose str contain 200 heap address and we set str = nil, then automatically weak property reference set to nil by the compiler. 假设str包含200个堆地址,我们设置str = nil,然后由编译器自动将弱属性引用设置为nil。

So that's the reason in stoary board ref controller, Main view only set to strong and other are weak for ex- we can see UIButton,UILabel out let etc 所以这就是stoary board ref controller的原因,主视图只设置为strong而其他弱者为ex-我们可以看到UIButton,UILabel out let等

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

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