文章40 | 阅读 19092 | 点赞0
class Traveler {
var name: String
var destination: String?
init(name:String) {
self.name = name
}
}
func test() {
let travel1 = Traveler(name: "Kody") // ①
// Retain
let travel2 = travel1 // ②
// Release // ③
travek2.destination = "Big Sur" // ④
// Release
print("Done traveling")
}
Traveler deinit ........
Done traveling
class Traveler {
var name: String
var account: Account?
init(name:String) {
self.name = name
}
}
class Account {
weak var traveler: Traveler?
var points: Int
init(points: Int, traveler: Traveler?) {
self.traveler = traveler
self.points = points
}
func printSummary() {
if let travel = traveler {
print("\(travel.name) has \(points) points")
}
}
}
func test() {
let travel = Traveler(name: "Kody")
let account = Account(points: 1000, traveler: travel)
travel.account = account
account.printSummary()
}
func test() {
let travel = Traveler(name: "Kody")
let account = Account(points: 1000, traveler: travel)
travel.account = account
withExtendedLifetime(travel, {
account.printSummary()
})
}
func test() {
let travel = Traveler(name: "Kody")
let account = Account(points: 1000, traveler: travel)
defer {withExtendedLifetime(travel, {
})}
travel.account = account
account.printSummary()
}
class Traveler {
var name: String
var account: Account?
init(name:String) {
self.name = name
}
func printSummary() {
if let account = account {
print("\(name) has \( account.points) points")
}
}
}
class Account {
private weak var traveler: Traveler?
var points: Int
init(points: Int, traveler: Traveler?) {
self.traveler = traveler
self.points = points
}
}
func test() {
let travel = Traveler(name: "Kody")
let account = Account(points: 1000, traveler: travel)
travel.account = account
travel.printSummary()
}
class PersonalInfo {
var name: String
}
class T raveler {
var info: PersonalInfo
var account: Account?
}
class Account {
var info: PersonalInfo
var points: Int
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/Forever_wj/article/details/121340881
内容来源于网络,如有侵权,请联系作者删除!