- iOS Programming Cookbook
- Hossam Ghareeb
- 132字
- 2021-07-09 18:29:44
How to do it...
- Create two classes, Person and Dog, with a relation between them, as shown in the following code snippet (this code snippet has a memory issue called reference cycle):
class Dog{ var name: String var owner: Person! init(name: String){ self.name = name } } class Person{ var name: String var id: Int var dogs = [Dog]() init(name: String, id: Int){ self.name = name self.id = id } } let john = Person(name: "John", id: 1) let rex = Dog(name: "Rex") let rocky = Dog(name: "Rocky") john.dogs += [rex, rocky] // append dogs rex.owner = john rocky.owner = john
- Update the reference type of owner property in the Dog class to break this cycle:
class Dog{ var name: String weak var owner: Person! init(name: String){ self.name = name } }
推薦閱讀
- 樂學Windows操作系統
- Windows Server 2012 Hyper-V:Deploying the Hyper-V Enterprise Server Virtualization Platform
- vSphere Virtual Machine Management
- 嵌入式Linux系統開發:基于Yocto Project
- Ganglia系統監控
- 嵌入式實時操作系統μC/OS原理與實踐
- 計算機系統的自主設計
- 跟老男孩學Linux運維:Shell編程實戰
- Windows 7實戰從入門到精通
- CentOS 6 Linux Server Cookbook
- Windows 7實戰從入門到精通(超值版)
- Linux網絡操作系統項目教程(RHEL 7.4/CentOS 7.4)(第3版)(微課版)
- Docker容器技術與應用
- Learn Quantum Computing with Python and IBM Quantum Experience
- Getting Started with UDK