ARC之解决对象之间的循环强引用(Swift)

前端之家收集整理的这篇文章主要介绍了ARC之解决对象之间的循环强引用(Swift)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_0@

@H_404_0@直接上代码,具体看代码中的注释

@H_404_0@

//
//  ViewController.swift
//  Refrence
//
//  Created by Mac on 8/24/15.
//  Copyright © 2015 fevershen. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    // 解决对象之间的循环强引用造成的内存泄漏
    
    // 1.弱引用, 2.无主引用, 3.无主引用以及显示展开(隐式解析)的可选属性
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        var john: Person
//        var number73: Apartment
//        john = Person(name: "John Appleseed")
//        number73 = Apartment(number: 73)
//        john.apartment = number73
//        number73.tenant = john
//        
        
//        var lina: Customer?
//        lina = Customer(name: "Lina Appleseed")
//        lina!.card = CreditCard(number: 1234_5678_9012_3456,customer: lina!)
        
        let country = Country(name: "Canada",captialName: "Ottawa")
        print("\(country.name)'s capital city is called \(country.capitalCity.name)")

        
        // 可选类型并不会自动初始化为 nil, 因此,使用前必须赋值
        let str: String? = nil
        if (str != nil) {
            print("===" + str!)
        } else {
            print("---")
        }
        
    }
    
    // 1.
    // 人和公寓的关系都是可选的, 这里用 weak 就可以了
    class Person {
        var name: String
        init (name: String) {
            self.name = name
            print("\(name) init")

        }
        var apartment: Apartment?
        deinit {
            print("\(name) deinit")
        }
    }
    
    class Apartment {
        let number: Int
        init (number: Int) {
            self.number = number
            print("Apartment \(number) init")

        }
        
        // 都不加 weak 将形成循环强引用,两个都不能被析构
        weak var tenant: Person?
        deinit {
            print("Apartment \(number) deinit")
        }
    }


    // 2.
    // 客户的信用卡是可选的,但是信用卡必须要有客户(非可选),因此这里要用到 unowner
    class Customer {
        let name: String
        var card: CreditCard?
        init(name: String) {
            self.name = name
            print("Customer \(name) init")
        }
        deinit {
            print("Customer \(name) deinit")
        }
    }
    
    class CreditCard {
        let number: Int
        
        // 卡必须有主人, 无主引用是永远有值的
        unowned let customer: Customer
        init(number: Int,customer: Customer) {
            self.number = number
            self.customer = customer
            print("CreditCard #\(number) init")

        }
        deinit {
            print("CreditCard #\(number) deinit")
        }
    }
    
    // 3.
    class Country {
        let name: String
        
        // 可选类型并不会自动初始化为 nil, 因此,使用前必须赋值
        // 这里的意义在于通过一条语句同时创建两个实例
        var capitalCity: City! = nil
        init(name: String,captialName: String) {
            print("Country is initing")
            self.name = name
            
            // 下面这条语句是将 Country 的实例做为参数传递给 City 来创建 City 的实例,表面上看 Country 本身还没有实例完成,实际上由于该类中 City 的属性为加 ! 的方式,因此,一旦 Country 的实例在构造函数中给 name 属性赋值,整个初始化过程就完成了,下面的打印信息为虚的
            self.capitalCity = City(name: captialName,country: self)
            print("Country is inited")
        }
        deinit {
            print("Country is deinited")
        }
    }
    
    class City {
        let name: String
        unowned let country: Country
        init(name: String,country: Country) {
            print("City is initing")
            self.name = name
            self.country = country
            print("City is inited")
        }
        deinit {
            print("City is deinited")
        }
    }
}


@H_404_0@3.打印结果:

@H_404_0@

Country is initing

City is initing

City is inited

Country is inited

Canada's capital city is called Ottawa

---

Country is deinited

City is deinited

猜你在找的Swift相关文章