Strong and weak references in Swift

风流意气都作罢 提交于 2019-12-02 18:44:04
Kaan Dedeoglu

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

A var is strong by default. You can add the weak keyword before a var to make it weak.

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

Just wanted you to know that a var is strong by default but by adding "weak" in front of it you make it weak. In case you missed it

This is more of an important comment, but I couldn't fit it in.

If you do

weak let name : SomeClass

It will give the following error:

'weak' must be a mutable variable, because it may change at runtime

You must do

weak var name : SomeClass

'weak' variable should have optional type 'SomeClass?'

So you must do:

weak var name : SomeClass?

Also, in Swift, all weak references are non-constant Optionals (think var vs. let) because the reference can and will be mutated to nil when there is no longer anything holding a strong reference to it. See here

As a result of this mandatory optional-ization, you always need to unwrap it so you can access its actual value.

Details

xCode 9.1, Swift 4

More info about ARC usage

Full sample

import UIKit

var str = "Hello, playground"

class BasicClass: CustomStringConvertible {
    let text: String
    init(text: String) { self.text = text }
    deinit { print ("Object of the \"\(className)\" class deinited") }

    var className: String {
        return "\(type(of: self))"
    }

    var referenceCount: Int {
        return CFGetRetainCount(self)
    }
    var description: String {
        return "className: \(className), reference count: \(referenceCount)"
    }
}

class Class1: BasicClass {
    var objectWithStrongReference: Class2?
    override var description: String {
        return super.description + ", embed strong obj reference count: \(objectWithStrongReference?.referenceCount ?? 0)"
    }
}

class Class2: BasicClass {
    weak var objectWithWeakReference: Class1?
    override var description: String {
        return super.description + ", embed weak obj reference count: \(objectWithWeakReference?.referenceCount ?? 0)"
    }
}

var obj1: Class1? = Class1(text: "String 1")
print(obj1 ?? "nil")
var obj2: Class2? = Class2(text: "String 2")
print(obj2 ?? "nil")

print("=====================================")
print("obj1.value = obj2, obj2.value = obj1")
obj1?.objectWithStrongReference = obj2
obj2?.objectWithWeakReference = obj1
print(obj1 ?? "nil")
print(obj2 ?? "nil")
print("=====================================")
print("obj2 = nil")
obj2 = nil
print(obj1 ?? "nil")
print(obj2 ?? "nil")

print("=====================================")
print("obj1 = nil")
obj1 = nil
print(obj1 ?? "nil")
print(obj2 ?? "nil")

Result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!