swift内存管理
为了解决引用循环的问题。
However, with ARC, values are deallocated as soon as their last strong reference is removed, making weak references unsuitable for such a purpose.
Unowned References
Like a weak reference, an unowned reference does not keep a strong hold on the instance it refers to. Unlike a weak reference, however, an unowned reference is used when the other instance has the same lifetime or a longer lifetime. You indicate an unowned reference by placing the unowned
keyword before a property or variable declaration.
An unowned reference is expected to always have a value. As a result, ARC never sets an unowned reference’s value to nil
, which means that unowned references are defined using nonoptional types.
IMPORTANT
Use an unowned reference only when you are sure that the reference always refers to an instance that has not been deallocated.
If you try to access the value of an unowned reference after that instance has been deallocated, you’ll get a runtime error.
声明周期:属性为nil时,本体也会消失;组合关系;
The Customer
and CreditCard
example shows a situation where one property that is allowed to be nil
and another property that cannot be nil
have the potential to cause a strong reference cycle. This scenario is best resolved with an unowned reference.
构造的哲学悖论:
To cope with this requirement, you declare the capitalCity
property of Country
as an implicitly unwrapped optional property, indicated by the exclamation mark at the end of its type annotation (City!
). This means that the capitalCity
property has a default value of nil
, like any other optional, but can be accessed without the need to unwrap its value as described in Implicitly Unwrapped Optionals.
Because capitalCity
has a default nil
value, a new Country
instance is considered fully initialized as soon as the Country
instance sets its name
property within its initializer. This means that the Country
initializer can start to reference and pass around the implicit self
property as soon as the name
property is set.
Don’t use an implicitly unwrapped optional when there’s a possibility of a variable becoming nil
at a later point. Always use a normal optional type if you need to check for a nil
value during the lifetime of a variable.
同步异步的问题:
If the captured reference will never become nil
, it should always be captured as an unowned reference, rather than a weak reference.
同步的用unowned,异步的用weak。
swift内存管理的更多相关文章
- Swift 内存管理详解
Swift内存管理: Swift 和 OC 用的都是ARC的内存管理机制,它们通过 ARC 可以很好的管理对象的回收,大部分的时候,程序猿无需关心 Swift 对象的回收. 注意: 只有引用类型变量所 ...
- swift内存管理中的引用计数
在swift中,每一个对象都有生命周期,当生命周期结束会调用deinit()函数进行释放内存空间. 观察这一段代码: class Person{ var name: String var pet: P ...
- Swift内存管理-示例讲解
具体而言,Swift中的ARC内存管理是对引用类型的管理,即对类所创建的对象采用ARC管理.而对于值类型,如整型.浮点型.布尔型.字符串.元组.集合.枚举和结构体等,是由处理器自动管理的,程序员不需要 ...
- swift 内存管理,WEAK 和 UNOWNED
因为 Playground 本身会持有所有声明在其中的东西,因此本节中的示例代码需要在 Xcode 项目环境中运行.在 Playground 中可能无法得到正确的结果. 不管在什么语言里,内存管理的内 ...
- Swift 内存管理
1.Object-C 经历两个阶段: 1.手动引用计数内存管理(Manual Reference Counting,MRC) 2.自动引用计数内存管理(Automatic Refernce Count ...
- Swift内存管理、weak和unowned以及两者区别
Swift 是自动管理内存的,这也就是说,我们不再需要操心内存的申请和分配.当我们通过初始化创建一个对象时,Swift 会替我们管理和分配内存.而释放的原则遵循了自动引用计数 (ARC) 的规则:当一 ...
- Swift内存管理、weak和unowned以及两者区别(如何使用Swift 中的weak与unowned?)
Swift 是自动管理内存的,这也就是说,我们不再需要操心内存的申请和分配. 当我们通过初始化创建一个对象时,Swift 会替我们管理和分配内存.而释放的原则遵循了自动引用计数 (ARC) 的规则:当 ...
- swift内存管理:值类型与引用类型
Use struct to create a structure. Structures support many of the same behaviors as classes, includin ...
- Swift中的可选链与内存管理(干货系列)
干货之前:补充一下可选链(optional chain) class A { var p: B? } class B { var p: C? } class C { func cm() -> S ...
随机推荐
- jmeter的认识
jmeter JMeter是Apache组织开发的开源项目,设计之初是用于做性能测试的,同时它在实现对各种接口的调用方面做的比较成熟,因此,常被用做接口功能测试和性能测试. 它能够很好的支持各种常见接 ...
- 详解循环神经网络(Recurrent Neural Network)
本文结构: 模型 训练算法 基于 RNN 的语言模型例子 代码实现 1. 模型 和全连接网络的区别 更细致到向量级的连接图 为什么循环神经网络可以往前看任意多个输入值 循环神经网络种类繁多,今天只看最 ...
- C# Distanct List集合
简单一维集合的使用 List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 }; List<str ...
- golden gate的DDL配置
DDL复制的配置 目前只支持oracle和teradata的ddl复制 oracle能复制除了系统对象之外的所有对象 两种配置方法: 基于trigger的DDL:对于生产库有一定影响. 原理: 源库建 ...
- WebApp开发技巧大全
1.开发成本较低使用web开发技术就可以轻松的完成web app的开发 2.升级较简单升级不需要通知用户,在服务端更新文件即可,用户完全没有感觉 3.维护比较轻松和一般的web一样,维护比较简单,它其 ...
- Django-admin源码解析
启动 <1>启动django,运行manage.py文件,进行当前项目的环境配置 <2>按照INSTALLED_APPS中的顺序加载APP,首先加载admin 注册 <1 ...
- redis 篇 - hash
hash 可以认为是 python 中的字典 field 不允许重复 string类型的field和value的映射表 每个hash可以存储 232 - 1 键值对(40多亿) 方法 hest key ...
- Windows Vista 安装和使用指导 - 停止支持后的几条建议
简介 曾经被广大网民吐槽的Windows Vista现在已经淡出了人们的视线,但仍有一些朋友想要体验一下这个操作系统.Windows Vista是Windows发展路线上的里程碑,相比之前的Windo ...
- 虚拟机安装mac
没成功,把几篇不错的文章先记录下地址 http://bbs.pcbeta.com/forum.php?mod=viewthread&tid=1437039 http://bbs.pcbeta. ...
- hive的mysql作元数据的hive-site.xml配置
<property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://s ...