Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Chris Sehnert
Chris Sehnert
30,857 Points

Strong Reference Cycles......the code is from a simple playground example that is supposed to demonstrate weak var usage

// the following example does not function the way the book describes.... // ...in my playground johnBoy continues to exist after he has been assigned nil..... /______________________________________________/

class NewerPerson { let name: String init(name: String) { self.name = name } var apartment: DifferentApartment? deinit { print("(name) is being deinitialized") } }

class DifferentApartment { let unit: String init(unit: String) { self.unit = unit } weak var tenant: NewerPerson? deinit { print("Apartment (unit) is being deinitialized") } }

var johnBoy: NewerPerson? var unit4C: DifferentApartment?

johnBoy = NewerPerson(name: "John-Boy") unit4C = DifferentApartment(unit: "4C")

johnBoy!.apartment = unit4C unit4C!.tenant = johnBoy

johnBoy = nil

var fourCTenant = unit4C!.tenant fourCTenant!.name var what = fourCTenant!.apartment

what!.unit var thatGuy = what!.tenant thatGuy!.name

print("as far as I can tell this weak reference is not working as advertised.....")

johnBoy thatGuy

/---------------------------------------------------/