博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
swift内存管理
阅读量:6180 次
发布时间:2019-06-21

本文共 2340 字,大约阅读时间需要 7 分钟。

为了解决引用循环的问题。

 

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 .

 

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。

 

转载地址:http://cxbda.baihongyu.com/

你可能感兴趣的文章
在MyEclipse中改动jsp页面的默认打开方式
查看>>
容器生态系统 - 每天5分钟玩转容器技术(2)
查看>>
面试总结二
查看>>
Python 之 读取txt文件
查看>>
Input系统—ANR原理分析(转)
查看>>
【云快讯】之四十八《IBM和Cisco最新收购,加强Openstack易用能力》
查看>>
书剑恩仇录online全套源代码(服务端+client+文档)
查看>>
百度地图 key申请以及基础地图的演示
查看>>
valid-palindrome——判断带符号数字字母的字符串是否为回文
查看>>
【iCore1S 双核心板_ARM】例程四:USART通信实验——通过命令控制LED
查看>>
unordered_map 与 map 的对比(转)
查看>>
构建单页Web应用
查看>>
【iCore1S 双核心板_ARM】例程十:SYSTICK定时器实验——定时点亮LED
查看>>
Re:从 0 开始的微服务架构--(三)微服务架构 API 的开发与治理--转
查看>>
聊一聊 Android 6.0 的运行时权限
查看>>
「mysql优化专题」什么是慢查询?如何通过慢查询日志优化?(10)
查看>>
肿瘤精准细胞免疫治疗:梦想照进现实
查看>>
DataGridView控件绑定数据源
查看>>
[原创]C/C++语言中,如何在main.c或main.cpp中调用另一个.c文件
查看>>
Lucene解析 - 基本概念
查看>>