问题
In Objective C you can define a property as having a strong or weak reference like so:
@property(strong)...
@property(weak)...
How is this done in swift?
回答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
回答2:
A var is strong by default. You can add the weak keyword before a var to make it weak.
回答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
回答4:
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
回答5:
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.
回答6:
Details
xCode 9.1, Swift 4
More info about ARC usage
- Kaan Dedeoglu answer
- ARC and Memory Management in Swift
- "WEAK, STRONG, UNOWNED, OH MY!" - A GUIDE TO REFERENCES IN SWIFT
- ARC Swift Tutorial
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
来源:https://stackoverflow.com/questions/24016527/strong-and-weak-references-in-swift