Last active
December 26, 2019 08:27
-
-
Save TerryCK/84bdb8835c1d3e6780bcc95f19e628a4 to your computer and use it in GitHub Desktop.
retainCyclePlayground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Two object retain cycle pattern | |
class Person: NSObject { | |
var name: String | |
var deparment: Department? | |
init(name:String) { | |
self.name = name | |
} | |
deinit { | |
print("person deinit") | |
} | |
} | |
class Department: NSObject { | |
var resident: Person? | |
// resolution | |
// weak var resident: Person? | |
var address: String | |
init(address: String) { | |
self.address = address | |
} | |
deinit { | |
print("Department deinit") | |
} | |
} | |
var terry: Person? = Person(name: "Terry") | |
var kyotoTown: Departure? = Departure(address: "kyoto road") | |
terry?.deparment = kyotoTown | |
kyotoTown?.resident = terry | |
terry = nil | |
kyotoTown = nil | |
// class Person & KyotoTown can't deinit. retain cycle occurred |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment