Swift 2 学习笔记 21.内存管理

课程来自慕课网liuyubobobo老师


内存管理
  • deinit
class Person {
    var name: String
    
    init(name: String) {
        self.name = name
    }
    
    deinit {
        print(self.name,"is leaving")
    }
}

var bobo: Person? = Person(name: "bobo")
bobo = nil  // bobo is leaving
  • 引用计数
class Person {
    var name: String
    var pet: Pet?
    
    init(name: String) {
        self.name = name
         print("Person is initialized!")
    }
    
    init(name: String, petname: String) {
        self.name = name
        self.pet = Pet(name: petname)
        print("Person is initialized!")
    }
    deinit {
        print("Person is deinitialized!")
    }
}

class Pet {
    var name: String
    
    init(name: String) {
        self.name = name
        print("Pet is initialized!")
    }
    
    deinit {
        print("Pet is deinitialized!")
    }
}

var bobo: Person? = Person(name: "bobo", petname: "aZai")
bobo = nil  // aZai 引用计数为0

var bobo2: Person? = Person(name: "bobo2")
var aZai2: Pet? = Pet(name: "aZai")
bobo2?.pet = aZai2
bobo2 = nil  // aZai 引用计数为1
  • 强引用循环和weak
class Person {
    var name: String
    var apartment: Apartment?

    init(name: String) {
        self.name = name
        print(name,"is initialized")
    }
    
    deinit {
        print("Person",name,"is deinitialized")
    }
    
}

class Apartment {
    let name: String
    weak var tenant: Person?  // weak用于可选型
    
    init(name: String) {
        self.name = name
        print(name,"is initialized")
    }
    
    deinit {
        print("Apartment",name,"is deinitialized")
    }
}

var bobo: Person? = Person(name: "bobo")
var imoocApartment: Apartment? = Apartment(name: "imooc")

bobo?.apartment = imoocApartment
imoocApartment?.tenant = bobo

imoocApartment = nil
bobo = nil

// bobo is initialized
// imooc is initialized
// Person bobo is deinitialized
// Apartment imooc is deinitialized
  • unowned
class Person {
    var name: String
    var creditCard: CreditCard?

    init(name: String) {
        self.name = name
        print(name,"is initialized")
    }
    
    deinit {
        print("Person",name,"is deinitialized")
    }
    
}

class CreditCard {
    let number: String
    unowned let customer: Person  // unowend用于非可选型
    
    init(number: String, customer: Person) {
        self.number = number
        self.customer = customer
        print("Credit Card",number,"is initialized")
    }
    
    deinit {
        print("Credit Card",number,"is deinitialized")
    }
}

var bobo: Person? = Person(name: "bobo")
var goldenCard: CreditCard? = CreditCard(number: "1234", customer: bobo!)

bobo?.creditCard = goldenCard

bobo = nil
goldenCard = nil
// bobo is initialized
// Credit Card 1234 is initialized
// Person bobo is deinitialized
// Credit Card 1234 is deinitialized
  • 使用隐式可选型
class Country {
    let name: String
    var capital: City!  // 允许并只在初始化时暂时为空
    
    init(countryName: String, capitalName: String) {
        self.name = countryName
        self.capital = City(cityName: capitalName, country: self)
        print("Country",name,"is initialized")
    }
    
    deinit {
        print("Country",name,"is deinitialized")
    }
}

class City {
    let name: String
    unowned let country: Country
    
    init(cityName: String,country: Country) {
        self.name = cityName
        self.country = country
        print("City",name,"is initialized")
    }
    
    deinit {
        print("City",name,"is deinitialized")
    }
}

var china: Country? = Country(countryName: "China", capitalName: "Beijing")
china = nil
  • 闭包中的强引用循环问题
class SmartAirConditioner {
    var temperature: Int = 26
    var temperatureChange: ((Int) -> ())!
    
    init() {
        temperatureChange = { [unowned self] newTemperature in
            if abs(newTemperature - self.temperature) > 10 {
                print("no")
            }else {
                self.temperature = newTemperature
                print("yes")
            }
        }
    }
    
    deinit {
        print("deinit")
    }
}

var airCon: SmartAirConditioner? = SmartAirConditioner()
airCon?.temperatureChange(100)
airCon = nil

你可能感兴趣的:(Swift 2 学习笔记 21.内存管理)