本文翻译自: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。

Objective-C中强项和弱项之间的差异相关推荐

  1. 一元流量参数为null_了解JavaScript中null和undefined之间的差异和相似之处

    干货技术→JavaScript 乍一看,null和undefined看起来可能相同,但它们远非如此.本文将探讨JavaScript中null和undefined之间的差异和相似之处. 如果你对编程感兴 ...

  2. 如何快速找出找出两个数组中的_找出JavaScript中两个数组之间的差异

    LeetCode今天面临的挑战是在数组中查找所有消失的数字. 蛮力 我们的输入包括一个缺少数字的实际数组.我们想将该数组与相同长度的数组进行比较,其中没有遗漏的数字.所以如果给定的话[4,3,2,7, ...

  3. python的timeit_Python中Timeit和Time之间的差异

    以下是否有任何显着差异: from time import time start = time() # some process print time() - start 和: from timeit ...

  4. python两组数的差异_Python中两个日期之间的差异

    我尝试了上面larsmans发布的代码,但是有两个问题: 1)原样的代码将引发mauguerra提到的错误2)如果将代码更改为以下内容: ... d1 = d1.strftime("%Y-% ...

  5. 神经网络中Batch和Epoch之间的区别

    来自蜂口知道公众号 随机梯度下降法是一种具有大量超参数的学习算法.通常会使初学者感到困惑的两个超参数: Batch大小和Epoch数量,它们都是整数值,看起来做的事情是一样的.在这篇文章中,您将发现随 ...

  6. 智能监控中隐私与安全之间的错误选择

    ACLU并没有错:某些技术确实可以促进这种反乌托邦的愿景. (The ACLU isn't wrong: Some technology could indeed contribute to this ...

  7. -ms-flexbox_Flexbox中width和flex-basis之间的区别

    -ms-flexbox by Kyle Gallagher 凯尔·加拉格尔(Kyle Gallagher) Flexbox中width和flex-basis之间的区别 (The difference ...

  8. Python中怎样改变集合之间的关系?

    Python中怎样改变集合之间的关系?数学中,两个集合关系的常见操作包括:交集.并集.差集.补集.设A,B是两个集合,集合关系的操作介绍如下: 交集是指属于集合A且属于集合B的元素所组成的集合, 并集 ...

  9. 深度学习中用于张量重塑的 MLP 和 Transformer 之间的差异图解

    来源:Deephub Imba本文约1800字,建议阅读5分钟 本文介绍在设计神经网络时如何解决张量整形的问题. 在设计神经网络时,我们经常遇到张量整形的问题.张量的空间形状必须通过改变某一层来适应下 ...

最新文章

  1. 什么,PyTorch还能开发新药?哈佛推出这款工具包,10行代码训练“药神”模型...
  2. linux 下使用 curl 访问带多参数,GET掉参数解决方案
  3. 09、redis哨兵的多个核心底层原理的深入解析(包含slave选举算法)
  4. 第四章:4.2MySQL 权限系统介绍
  5. SpringMVC+Mybatis基础知识和配置
  6. AAAI 2021 | 基于对比学习的聚类工作
  7. 彻底弄懂css3的flex弹性盒模型
  8. CodeForces - 1303E Erase Subsequences(dp)
  9. 马哥linux 教程---vim编辑器进阶课后题
  10. Spring Boot(2)---Spring Boot文档及其导航说明
  11. 防止数据中心停机需要采取什么措施
  12. nssm在服务器上的使用超详细说明
  13. vs2010 破解版,试用版变正式版
  14. C# PPT/PDF文件转图片,图片转PPT
  15. 计算机等级考试 设置表格居中,Word表格水平居中怎么设置
  16. 为什么五笔输入法打字那么快,现在使用的人却越来越少了?
  17. java线程状态研究
  18. 辰视智能董事长冯良炳博士接受CA168中自网专访
  19. html5 java 实现微信公众号自动分享功能(自定义文案和图标)
  20. 【Python实战】--批量下载百度图库图片

热门文章

  1. 青岛海尔2018年实现净利润74.4亿元 净利率约4.06%
  2. 虚电路服务和无连接的数据包服务
  3. 编译原理实验三 语义分析程序设计与实现
  4. 如何获取触摸精灵的版本号??
  5. 【06月04日】A股滚动市盈率PE历史新低排名
  6. 全手动搭建Kubernetes集群——Master管理节点和Node工作节点部署
  7. IRIG-B码对时理解
  8. 极客DIY:只用两步教你制作一款可编程键盘 [译]
  9. dhcp 的option说明
  10. QQ找茬辅助源码下载