前端之家收集整理的这篇文章主要介绍了
Swift基础篇——内存相关,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
内存相关
import Foundation
class Person {
var name:String
init(name:String){
self.name = name
}
deinit{
print("deinit")
}
}
var p:Person? = Person(name: "Swift")
class Person2 {
var name:String
init(name:String){
"deinit")
}
}
var strongP = Person2(name: "Swift")
var strongP2 = strongP
weak var weakP:Person2? = Person2(name: "Swift")
if let p = weakP{
print(p)
}else
{
print(weakP)
}
class Person3 {
var name:String
init(name:String){
"deinit")
}
}
unowned var weakP3:Person3 = Person3(name: "Swift")
class Person4 {
let name:String
weak var apartment: Apartment?
init(name:String){
"\(self.name) deinit")
}
}
class Apartment {
let number: Int
var tenant: Person4?
init(number:Int){
.number = number
}
deinit{
print("\(self.number) deinit")
}
}
var p4:Person4? = Person4(name: "Swift")
var a4:Apartment? = Apartment(number:888)
p4!.apartment = a4
a4!.tenant = p4!
p4 = nil
a4 = nil
class Person5 {
let name:String
var card: CreditCard?
init(name:String){
"\(self.name) deinit")
}
}
class CreditCard{
let number: Int
unowned let person: Person5
init(number:Int,person: Person5){
.number = number
.person = person
}
deinit{
print("\(self.number) deinit")
}
}
var p5:Person5? = Person5(name: "Swift")
var cc:CreditCard? = CreditCard(number: 8888888,person: p5!)
p5 = nil
cc = nil
原文链接:https://www.f2er.com/swift/325655.html