Objective-C中强项和弱项之间的差异

本文翻译自:Differences between strong and weak in Objective-C

I'm new to Obj-C, so my first question is: 我是Obj-C的新手,所以我的第一个问题是:

What are the differences between strong and weak in @property declarations of pointers to objects? @property指针声明的strongweak之间有什么区别?

Also, what does nonatomic mean? 另外, nonatomic是什么意思?


#1楼

参考:https://stackoom.com/question/kD8V/Objective-C中强项和弱项之间的差异


#2楼

strong : assigns the incoming value to it, it will retain the incoming value and release the existing value of the instance variable strong :为其分配传入值,它将保留传入值并释放实例变量的现有值

weak : will assign the incoming value to it without retaining it. :将分配传入的值而不保留它。

So the basic difference is the retaining of the new variable. 因此,基本区别是保留新变量。 Generaly you want to retain it but there are situations where you don't want to have it otherwise you will get a retain cycle and can not free the memory the objects. 通常,您想保留它,但是在某些情况下,您不想保留它,否则您将获得保留周期,并且无法释放对象的内存。 Eg. 例如。 obj1 retains obj2 and obj2 retains obj1. obj1保留obj2,而obj2保留obj1。 To solve this kind of situation you use weak references. 为了解决这种情况,您可以使用弱引用。


#3楼

A strong reference (which you will use in most cases) means that you want to "own" the object you are referencing with this property/variable. 强大的引用(在大多数情况下将使用)表示您要“拥有”使用此属性/变量引用的对象。 The compiler will take care that any object that you assign to this property will not be destroyed as long as you point to it with a strong reference. 编译器会注意,只要您使用强引用指向该对象,分配给该属性的任何对象都不会被破坏。 Only once you set the property to nil will the object get destroyed (unless one or more other objects also hold a strong reference to it). 仅当将属性设置为nil时,该对象才会被销毁(除非一个或多个其他对象也对该对象具有强烈的引用)。

In contrast, with a weak reference you signify that you don't want to have control over the object's lifetime. 相反,使用弱引用意味着您不希望控制对象的生命周期。 The object you are referencing weakly only lives on because at least one other object holds a strong reference to it. 您所弱引用的对象仅能生存,因为至少有另一个对象对此对象具有强引用。 Once that is no longer the case, the object gets destroyed and your weak property will automatically get set to nil . 一旦不再是这种情况,对象将被销毁,并且您的弱属性将自动设置为nil The most frequent use cases of weak references in iOS are: iOS中弱引用的最常见用例是:

  1. delegate properties, which are often referenced weakly to avoid retain cycles, and 委派属性,这些属性通常被弱引用以避免保留周期,并且

  2. subviews/controls of a view controller's main view because those views are already strongly held by the main view. 视图控制器主视图的子视图/控件,因为这些视图已经由主视图牢牢占据。

atomic vs. nonatomic refers to the thread safety of the getter and setter methods that the compiler synthesizes for the property. 原子与非原子是指编译器为该属性综合的getter和setter方法的线程安全性。 atomic (the default) tells the compiler to make the accessor methods thread-safe (by adding a lock before an ivar is accessed) and nonatomic does the opposite. 原子(默认)告诉编译器使访问器方法成为线程安全的(通过在访问ivar之前添加锁),而非原子的则相反。 The advantage of nonatomic is slightly higher performance. 非原子的优点是性能略高。 On iOS, Apple uses nonatomic for almost all their properties so the general advice is for you to do the same. 在iOS上,Apple几乎对所有属性都使用非原子性,因此一般建议您也这样做。


#4楼

Here, Apple Documentation has explained the difference between weak and strong property using various examples : 在这里, Apple文档使用各种示例解释了弱属性和强属性之间的区别:

https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW3 https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW3

Here, In this blog author has collected all the properties in same place. 在这里,该博客作者在同一位置收集了所有属性。 It will help to compare properties characteristics : 这将有助于比较属性特征:

http://rdcworld-iphone.blogspot.in/2012/12/variable-property-attributes-or.html http://rdcworld-iphone.blogspot.in/2012/12/variable-property-attributes-or.html


#5楼

It may be helpful to think about strong and weak references in terms of balloons. 考虑气球的强引用和弱引用可能会有所帮助。

A balloon will not fly away as long as at least one person is holding on to a string attached to it. 只要至少有一个人抓住气球上的绳子,气球就不会飞走。 The number of people holding strings is the retain count. 持有琴弦的人数是保留人数。 When no one is holding on to a string, the ballon will fly away (dealloc). 当没有人抓住弦时,气球将飞走(取消分配)。 Many people can have strings to that same balloon. 许多人可以在同一气球上放绳子。 You can get/set properties and call methods on the referenced object with both strong and weak references. 您可以使用强引用和弱引用来获取/设置被引用对象的属性并调用方法。

A strong reference is like holding on to a string to that balloon. 强有力的参考就像坚持那个气球的弦一样。 As long as you are holding on to a string attached to the balloon, it will not fly away. 只要您抓住气球上的绳子,它就不会飞走。

A weak reference is like looking at the balloon. 弱引用就像看着气球。 You can see it, access it's properties, call it's methods, but you have no string to that balloon. 您可以看到它,访问它的属性,调用它的方法,但是该气球没有任何字符串。 If everyone holding onto the string lets go, the balloon flies away, and you cannot access it anymore. 如果所有人都松开了绳子,气球就会飞走,您将无法再使用它。


#6楼

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. 指定一个引用,该引用不能使引用的对象保持活动状态。 A weak reference is set to nil when there are no strong references to the object. 如果没有对对象的强引用,则将弱引用设置为nil。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值