Skip to content

Instantly share code, notes, and snippets.

@vialyx
Created April 24, 2018 08:30
Show Gist options
  • Save vialyx/0f8b34dc7bbafff0fa40daf0efa3b631 to your computer and use it in GitHub Desktop.
Save vialyx/0f8b34dc7bbafff0fa40daf0efa3b631 to your computer and use it in GitHub Desktop.
// MARK: - weak reference
weak var weakCustomer1stRef: Customer? = nil
weak var weakCustomer2stRef: Customer? = nil
weakCustomer1stRef = Customer(udid: "weak 1", name: "Jacob")
/*
print: weak 1 initialized
print: weak 1 deinitialized
*/
/*
That is deallocated right after initialization because haven't any strong reference.
weakCustomer1stRef is nil
*/
var strongCustomerRef: Customer? = Customer(udid: "strong 1", name: "Klo")
/*
strong 1 initialized
*/
weakCustomer1stRef = strongCustomerRef
weakCustomer2stRef = weakCustomer1stRef
print("weak 1st: \(String(describing: weakCustomer1stRef))")
print("weak 2nd: \(String(describing: weakCustomer2stRef))")
/*
Both weak references are not nil
*/
strongCustomerRef = nil
/*
print: strong 1 deinitialized
*/
print("weak 1st: \(String(describing: weakCustomer1stRef))")
print("weak 2nd: \(String(describing: weakCustomer2stRef))")
/*
strongCustomerRef is setup to nil
Both weak references will be setup to nil at runtime
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment