Swift中的强引用和弱引用

时间:2014-06-03 13:31:31

标签: objective-c reference weak-references swift

在Objective C中,您可以将属性定义为具有强引用或弱引用,如下所示:

@property(strong)...
@property(weak)...

这是如何在swift中完成的?

6 个答案:

答案 0 :(得分:62)

直接来自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") }
}
默认情况下,

属性很强。但是看看“公寓”这个类的租户属性,它被宣称为弱者。您还可以使用unowned关键字,该关键字转换为Objective-C

中的unsafe_unretained

https://itunes.apple.com/tr/book/swift-programming-language/id881256329?mt=11

答案 1 :(得分:12)

默认情况下,var很强。您可以在var之前添加weak关键字以使其变弱。

答案 2 :(得分:10)

默认情况下属性很强,但如果你想要一个弱属性,你可以:

    weak var tenant: Person?

来源:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html

答案 3 :(得分:4)

只是想让你知道默认情况下var是强的,但是通过在它前面添加“弱”你会使它变弱。万一你错过了

答案 4 :(得分:4)

这是一个重要的评论,但我无法适应它。

如果你这样做

weak let name : SomeClass

会出现以下错误:

  

'weak'必须是一个可变变量,因为它可能在运行时发生变化

你必须

weak var name : SomeClass
  

'weak'变量应该有可选类型'SomeClass?'

所以你必须这样做:

weak var name : SomeClass?

此外,在Swift中,所有弱引用都是非常量的Optionals(想想var与let),因为当不再有任何强引用时,引用可以并且将会变为nil。见here

由于这种强制性的可选化,您始终需要解包,以便您可以访问其实际值。

答案 5 :(得分:0)

详细

xCode 9.1,Swift 4

有关ARC使用的更多信息

完整样本

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")

结果

enter image description here