Strong and weak references in Swift
Solution 1:
Straight from the Swift Language guide:
class Person {
let name: String
init(name: String) { self.name = name }
var apartment: Apartment?
deinit { println("\(name) is being deinitialized") }
}
class Apartment {
let number: Int
init(number: Int) { self.number = number }
weak var tenant: Person?
deinit { println("Apartment #\(number) is being deinitialized") }
}
properties are strong by default. But look at the tenant property of the class "Apartment", it is declared as weak. You can also use the unowned keyword, which translates to unsafe_unretained from Objective-C
https://itunes.apple.com/tr/book/swift-programming-language/id881256329?mt=11
Solution 2:
A var is strong by default. You can add the weak keyword before a var to make it weak.
Solution 3:
Properties are strong by default, but if you want a weak property you can:
weak var tenant: Person?
Source: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html