1. 析构器的定义:
class Bank {
static var coinsInBank = 10_000
static func distribute(coins numberOfCoinsRequested: Int) -> Int {
let numberOfCoinsToVend = min(numberOfCoinsRequested, coinsInBank)
coinsInBank -= numberOfCoinsToVend
return numberOfCoinsToVend
}
// 定义析构器
deinit {
// 析构器的实现
print("Bank is being deinitialized. Remaining coins: \(Bank.coinsInBank)")
}
}
2. 实例的释放:
当一个类的实例被释放时,其析构器会被调用。这通常是由于实例的引用计数降为零导致的。
var playerOne: Bank? = Bank()
print("Coins requested: \(Bank.distribute(coins: 4))") // 输出 "Coins requested: 4"
// playerOne 被设置为 nil,导致 Bank 实例被释放
playerOne = nil // 输出 "Bank is being deinitialized. Remaining coins: 9996"
3. 弱引用和无主引用:
在闭包和循环引用中,可以使用弱引用(weak)和无主引用(unowned)来防止循环引用并避免强引用造成的内存泄漏。
class Apartment {
var tenant: Person?
deinit {
print("Apartment is being deinitialized")
}
}
class Person {
var apartment: Apartment?
deinit {
print("Person is being deinitialized")
}
}
var john: Person? = Person()
var unit4A: Apartment? = Apartment()
john?.apartment = unit4A
unit4A?.tenant = john
john = nil
unit4A = nil
// 输出 "Person is being deinitialized" 和 "Apartment is being deinitialized"
在上面的例子中,Person 和 Apartment 之间形成了循环引用。通过使用弱引用或无主引用,可以解决这个问题。
4. 解除弱引用和无主引用:
对于弱引用,使用 weak 关键字;对于无主引用,使用 unowned 关键字。
class Customer {
let name: String
var card: CreditCard?
init(name: String) {
self.name = name
}
deinit {
print("Customer \(name) is being deinitialized")
}
}
class CreditCard {
let number: UInt64
unowned var customer: Customer
init(number: UInt64, customer: Customer) {
self.number = number
self.customer = customer
}
deinit {
print("Card #\(number) is being deinitialized")
}
}
var johnDoe: Customer? = Customer(name: "John Doe")
johnDoe?.card = CreditCard(number: 1234_5678_9012_3456, customer: johnDoe!)
johnDoe = nil
// 输出 "Customer John Doe is being deinitialized" 和 "Card #1234567890123456 is being deinitialized"
在上面的例子中,使用 unowned 关键字来定义 customer 属性,表示 CreditCard 实例的生命周期取决于 Customer 实例。
析构器是确保资源正确释放的关键组成部分,尤其是在涉及到引用循环时。
转载请注明出处:http://www.zyzy.cn/article/detail/6856/Swift