Swift一步步学习

Swift学习之内存管理

2020-01-13  本文已影响0人  coderhlt

一、引用管理

和OC一样,Swift也是采取基于引用计数的ARC内存管理

var s:Student? = Student()

weak  var s1:Student? = Student()
class Student {
    var age:Int = 0
}
unowned var s2:Student? = Student()
print(s2?.age)

Fatal error: Attempted to read an unowned reference but object 0x100600660 was already deallocated

二、循环引用

2.1

class Student {
    var p0:Person?
     deinit {
             print("销毁 ")
    }
}

class Person {
    var s0:Student?
    deinit {
           print("销毁 ")
       }
}
var s:Student? = Student()
var p:Person? = Person()

s?.p0 = p
p?.s0 = s

p = nil
s = nil

Person和Student都不会销毁,虽然指向Person对象的p强指针和指向Student对象的s强指针不再引用对象了,但是Person对象的属性s0指针强引用着Student对象,Student对象的属性
p0指针强引用着Person对象,这样就产生了相互引用

class Student {
   weak var p0:Person?
     deinit {
             print("销毁 ")
    }
}

想要打破相互引用的局面,对属性指针置弱就可以了。p0 用weak修饰,也就意味着p = nil ,Person对象没有被任何强指针引用,就会销毁,自然Student对象也就销毁了。

2.2、闭包的循环引用

class Person {
    var fn:(() -> ())? //闭包作为存储属性,声明格式:(函数类型)?
    var fn1:((Int) ->(Int))?
    func run() {
        print("run")
    }
    deinit {
        print("deinit")
    }
}
func test (){
    let p = Person()
    p.fn = {
        p.run()
    }
}
test()
p.fn = {
    [weak p] in
    p?.run()
}

   p.fn = {
        [unowned p] in
        p.run()
    }
class Person{
 lazy   var fn:(() -> ()) = {[weak self] in self?.run()}
func run(){print(“run”)}
deinit{print(“deinit")}
class Person{
  var age: Int = 0
lazy var getAge:Int = {
      self.age
}()

如果lazy属性是闭包调用的结果,那么不用考虑循环引用的问题(因为闭包调用后,闭包的生命周期就结束了 )

上一篇下一篇

猜你喜欢

热点阅读