Created
April 24, 2018 08:30
-
-
Save vialyx/0f8b34dc7bbafff0fa40daf0efa3b631 to your computer and use it in GitHub Desktop.
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
// 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