Swift基础篇——懒加载

前端之家收集整理的这篇文章主要介绍了Swift基础篇——懒加载前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

懒加载

import UIKit

class ViewController: UIViewController {
    // 只有外界访问到listData的时候才会去执行闭包,然后将闭包的返回值赋值给listData
    // 注意: 一定要记住闭包后面需要写上(),代表执行闭包
    lazy var listData: [String]? = {
        ()->[String]
        in
        print("----")
        return ["longshao","guangzhou",0)">"beijing",0)">"shenzhen"]
        }()

    // 开发中这样写
    lazy var listData2: [String]? = {
        print("shenzhen"]
    }()

    lazy var listData3: [String]? = self.test()
    func test() -> [String]
    {
        print("+++")
        "lnj1",0)">"lmj1",0)">"why1",0)">"wsz1"]
    }

    override func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?) {
        print(listData!)
        print(listData)
        print(listData)

        print("++++++++++++++++++++++++++++")

        print(listData2)
        print(listData2)
        print(listData2)

        print("++++++++++++++++++++++++++++")

        print(listData3)
        print(listData3)
        print(listData3)
    }
}

猜你在找的Swift相关文章