swif-自动引用计数

import UIKit


/*
class Person {
    let name: String
 
 //强引用
    init(name: String) {
        self.name = name
        print("\(name) is being initialized")
    }
 
 //引用计数销毁时
    deinit {
        print("\(name) is being deinitialized")
    }
}
*/

class Person {
    let name: String
    init(name: String) { self.name = name }
    var apartment: Apartment?
    deinit { print("\(name) is being deinitialized") }
}
class Apartment {
    let unit: String
    init(unit: String) { self.unit = unit }
//    var tenant: Person?

    weak var tenant: Person? //弱引用
    deinit { print("Apartment \(unit) is being deinitialized") }
}

class First_Demo2: UIViewController {
    
//    var reference1: Person?
//    var reference2: Person?
//    var reference3: Person?
    
    var john: Person?
    var unit4A: Apartment?
    var johnnn: Customer?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "自动引用计数"
        self.view.backgroundColor = UIColor.white
        
//        let person = Person(name:"wangyalu")
        
        //reference1 = Person(name: "John Appleseed")
        //John Appleseed is being initialized

        //在你清楚地表明不再使用这个 Person 实例,强引用被断开时,ARC 会销毁它:
//        reference1 = nil
        
        
        //互为强引用
        john = Person(name:"wang")
        unit4A = Apartment(unit:"4A")
        
        // Person 实例现在有了一个指向 Apartment 实例的强引 用,而 Apartment 实例也有了一个指向 Person 实例的强引用。因此,当你断开 john 和 unit4A 变量所持有的强 引用时,引用计数并不会降为 0 ,实例也不会被 ARC 销毁:

        john!.apartment = unit4A
        unit4A!.tenant = john
        
       //当你把这两个变量设为 nil 时,没有任何一个析构函数被调用。循环强引用会一直阻止 Person 和 Apartme nt 类实例的销毁,这就在你的应用程序中造成了内存泄漏。
        john = nil
        unit4A = nil
        
        //注意:
        /*在使用垃圾收 的系统里,弱指针有时用来实现简单的缓冲机制,因为没有强引用的对象只会在内存压力触发垃 圾收 时才被销毁。但是在 ARC 中,一旦值的最后一个强引用被移除,就会被立即销毁,这导致弱引用并不适 合上面的用途。*/
        
        johnnn = Customer(name: "John Appleseed")
        johnnn!.card = CreditCard(number: 1234_5678_9012_3456, customer: johnnn!)
        johnnn = nil
        // 打印 “John Appleseed is being deinitialized”
        // 打印 ”Card #1234567890123456 is being deinitialized”
    }
    
    /*
     Customer 实例持有对 CreditCard 实例的强引用,而 CreditCard 实例持有对 Customer 实例的无主引用。 由于 customer 的无主引用,当你断开 johnnn 变量持有的强引用时,再也没有指向 Customer 实例的强引用了:
     由于再也没有指向 Customer 实例的强引用,该实例被销毁了。其后,再也没有指向 CreditCard 实例的强引 用,该实例也随之被销毁了:
     */
    
    class Customer {
        let name: String
        var card: CreditCard?
        init(name: String) {
            self.name = name
        }
        deinit { print("\(name) is being deinitialized") }
    }
    class CreditCard {
        let number: UInt64
        unowned let customer: Customer
        init(number: UInt64, customer: Customer) {
            self.number = number
            self.customer = customer
        }
        deinit { print("Card #\(number) is being deinitialized") }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

 

 

posted @ 2016-12-30 15:49  代码始我快乐  阅读(140)  评论(0编辑  收藏  举报