内存管理不管是在ObjectC 还是swift上,在开发过程中都是我们需要关注的一个重点。刚好学习到,做此笔记
1.类的初始化和类的销毁
1.类初始化使用init关键字;
2.类的销毁使用deinit关键字。
eg:
class Student{
let name : String
var website = "JournalDev"
//初始化
init(name:String){
self.name = name
print("\(name) is being initialized")
}
//释放变量:自动释放,不能手动调用。
deinit {
print("\(name) is being deinitialized")
print("\(website) is being deinitialized")
}
}
var s : Student? = Student(name:"Chloe")
//打印结果:Chloe is being initialized
var reference = s//此时reference引用了s
var reference2 = reference//reference2引用了reference
//因为类是引用类型,此时,s被reference引用;reference被reference2引用;
reference = nil//reference is nil;没有走deinit方法
s = nil//s is nil;没有走deinit方法
reference2 = nil//走deinit方法
//打印结果:
//Chloe is being deinitialized
//JournalDev is being deinitialized
//从上面的例子可以看出,只有对类的引用全部无效时,类才被释放。
2.循环引用(强引用)
因为类是引用类型,所以很容易造成循环引用,导致类不能被释放;
eg:
class Major {
let name : String
var university : University?//引用了University类
init(name : String) {
self.name = name
}
deinit {
print("\(name) is being deinitialized")
}
}
class University{
let uniName : String
init(uniName : String) {
self.uniName = uniName
}
var major : Major?//引用了Major类
deinit {
print("University \(uniName) is being deinitialized")
}
}
//Major类和University此时是循环引用状态。现在我们对两个类进行初始化
var meMajor: Major?
var uni: University?
meMajor = Major(name: "John Doe")
uni = University(uniName: "IIT")
meMajor?.university = uni
uni?.student = meMajor
meMajor = nil
uni = nil
当我们给meMajor和uni都置为nil时,此时发现Major和University都没有被释放;然后我们看一下怎么解决。
先了解一下引用类型:
引用类型有三种:strong、weak、unowned reference;
(1)strong:强引用是默认情况下创建的引用的标准类型。
(2)weak:弱引用允许创建实例,但不计入弧的引用计数。通常,当一个引用的生存期比另一个引用短时,应该将其标记为弱引用。弱引用可以为nil。
(3)unowned references:无主引用与弱引用类似,只是它们用于比其他引用具有更长生存期的引用。只有在绝对确定引用确实存在时才应该使用无主引用,否则如果在nil引用上调用它,它将崩溃。也就是无主引用不可以置为nil。
解决办法:所以上面的循环引用,我们可以用弱引用或者无主引用来解决循环引用问题。只要在属性前面添加weak关键字就可以
class Major {
let name : String
weak var university : University?
init(name : String) {
self.name = name
}
deinit {
print("\(name) is being deinitialized")
}
}
class University{
let uniName : String
init(uniName : String) {
self.uniName = uniName
}
weak var major : Major?
deinit {
print("University \(uniName) is being deinitialized")
}
}
var meMajor: Major?
var uni: University?
meMajor = Major(name: "John Doe")
uni = University(uniName: "IIT")
meMajor?.university = uni
uni?.major = meMajor
meMajor = nil
uni = nil
//打印结果:
//University IIT is being deinitialized
//John Doe is being deinitialized
3.闭包循环引用
使用捕获列表解析闭包中的引用循环;闭包是引用类型,所以也会造成循环引用。
闭包中使用了[unowned self]和[weak self]都不会造成闭包循环引用问题。
class User {
let name: String
let skill: String
//会造成循环引用
// lazy var summary: () -> String = {
// return "\(self.name) (\(self.skill))"
// }
//不会造成循环引用
// lazy var summary: () -> String = {[unowned self] in
// return "\(self.name) (\(self.skill))"
// }
//也不会造成循环引用
lazy var summary:() -> String = {[weak self] in
guard let weakSelf = self else { return }
return "\(weakSelf.name) (\(weakSelf.skill))"
}
init(name: String, skill: String) {
self.name = name
self.skill = skill
}
deinit {
print("Deallocated User")
}
}
var name: User? = User(name: "Anupam", skill: "Swift")
name?.summary()
name = nil
网友评论