首页 > 代码库 > Swift 懒加载

Swift 懒加载

////  ViewController.swift//  懒加载////  Created by nanakon on 17/3/11.//  Copyright © 2017年 nanakon. All rights reserved.//import UIKitclass ViewController: UIViewController {        // 懒加载 用到时才加载 多次使用只加载一次    // 程序一启动就创建了这个数组占据内存空间    // var names : [String] = ["why", "lmj"]        //lazy var names : [String] = ["why", "lmj"]    lazy var names : [String] = {        let names = ["why", "lmj"]                print("用到names时加载names")                return names    }()        //lazy var btn : UIButton = UIButton()        lazy var btn : UIButton = {        let btn = UIButton()        btn.setTitle("按钮", for: .normal)        return btn    }()        override func viewDidLoad() {        super.viewDidLoad()    }        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {        print(names)        print(btn)    }}

 

Swift 懒加载