在Swift中,由于值类型在传递过程会创建新的副本,所以你不需要做任何事情管理值类型的内存;而引用类型则不一样,它在传递过程中会创建新的引用,这些引用都指向同一块内存,当引用计数为0时,实例就会被回收,deinit方法将被调用;和OC一样,Swift也使用了自动引用计数(ARC)来管理类实例的引用计数。
循环强引用
下面代码中xiaoMing拥有book和milk,而book和milk也拥有xiaoMing,造成了循环强引用,以至于最后只有house被释放了。
class Person{
let name:String
//拥有的资产
var assets=[Asset]()
init(name:String){
self.name=name
print("Person(\(name)) is created!")
}
deinit{
print("Person(\(name)) is destoryed!")
}
func takeAsset(asset:Asset){
asset.owner=self
assets.append(asset)
}
}
class Asset{
let name:String
//资产所有者
var owner:Person?
init(name:String){
self.name=name
print("Asset(\(name)) is created!")
}
deinit{
print("Asset(\(name)) is destoryed!")
}
}
var xiaoMing:Person?=Person(name: "Xiao Ming")
var book:Asset?=Asset(name: "book")
var milk:Asset?=Asset(name: "milk")
var house:Asset?=Asset(name: "house")
xiaoMing?.takeAsset(asset: book!)
xiaoMing?.takeAsset(asset: milk!)
book=nil
milk=nil
house=nil
xiaoMing=nil
//Person(Xiao Ming) is created!
//Asset(book) is created!
//Asset(milk) is created!
//Asset(house) is created!
//Asset(house) is destoryed!
弱引用
使用弱引用可以解决循环强引用问题,由于是Person拥有Asset,而不是Asset拥有Person,所以我们把Asset中的owner属性由默认的强引用修改为弱引用,再次运行代码发现实例都得到了正确释放;
class Asset{
let name:String
//资产所有者
weak var owner:Person?
init(name:String){
self.name=name
print("Asset(\(name)) is created!")
}
deinit{
print("Asset(\(name)) is destoryed!")
}
}
由于弱引用在指向的实例被释放后会置空,所以弱引用必须为可空类型且用var声明
闭包中的循环引用
下面代码中,给Person增加Asset的时候会给Asset的一个闭包实例赋值,而这个闭包中使用self引用了Person,同样造成了强引用循环。
class Person{
let name:String
//拥有的资产
var assets=[Asset]()
init(name:String){
self.name=name
print("Person(\(name)) is created!")
}
deinit{
print("Person(\(name)) is destoryed!")
}
func takeAssetCallback(){
print("takeAssetCallback called")
}
func takeAsset(asset:Asset){
asset.takeCallback={
self.takeAssetCallback()
}
assets.append(asset)
}
}
class Asset{
typealias TakeCallback = ()-> Void
let name:String
var takeCallback:TakeCallback?=nil
init(name:String){
self.name=name
print("Asset(\(name)) is created!")
}
deinit{
print("Asset(\(name)) is destoryed!")
}
}
var xiaoMing:Person?=Person(name: "Xiao Ming")
var book:Asset?=Asset(name: "book")
var milk:Asset?=Asset(name: "milk")
var house:Asset?=Asset(name: "house")
xiaoMing?.takeAsset(asset: book!)
xiaoMing?.takeAsset(asset: milk!)
book=nil
milk=nil
house=nil
xiaoMing=nil
显示声明self为弱引用解决闭包中的循环强引用问题
class Person{
let name:String
//拥有的资产
var assets=[Asset]()
init(name:String){
self.name=name
print("Person(\(name)) is created!")
}
deinit{
print("Person(\(name)) is destoryed!")
}
func takeAssetCallback(){
print("takeAssetCallback called")
}
func takeAsset(asset:Asset){
asset.takeCallback = {
[weak self] in
self?.takeAssetCallback();
}
assets.append(asset)
}
}
逃逸闭包和非逃逸闭包
上面代码中的闭包是逃逸闭包,而非逃逸闭包指的是不可能在函数返回后被调用的闭包,这种闭包不会产生循环强引用。
class Person{
let name:String
//拥有的资产
var assets=[Asset]()
init(name:String){
self.name=name
print("Person(\(name)) is created!")
}
deinit{
print("Person(\(name)) is destoryed!")
}
func takeAssetCallback(){
print("takeAssetCallback called")
}
func takeAsset(asset:Asset){
var callback = {
self.takeAssetCallback()
}
callback();
assets.append(asset)
}
}
网友评论