I believe the top-level function (REPL / playground) maintains a strong link to facilitate interactive behavior and cleanup when the frame returns. This behavior resolves memory leaks in an interactive environment.
I copied a simple Viktor simple example and used xcrun swift REPL.
In REPL mode, I wrapped the logic in a function and works as expected. If / when you care when the memory is cleared, I would suggest wrapping your logic in a function.
// declaration of the types class Person { let name: String weak var home: Apartment? init(pName: String){ name = pName } } class Apartment { let postalCode: Int init(pPostalCode: Int) { postalCode = pPostalCode } } func testArc() { // create Person object var personJulius: Person = Person(pName: "Julius") // create Apartment object var apartmentBerlin: Apartment? = Apartment(pPostalCode: 10777) // connect Apartment object and Person object personJulius.home = apartmentBerlin // Set only strong reference of Apartment object to nil apartmentBerlin = nil // Person object should now have nil as home if personJulius.home != nil { println("Julius does live in a destroyed apartment") } else { println("everything as it should") } } //outputs "everything as it should" testArc()
source share