Swift从入门到放弃

swift 懒加载

2018-05-02  本文已影响2735人  鹏飞说

特点

  1. 用到的时候才会加载 2.多次使用只会加载一次
class ViewController: UIViewController {
    
    // lazy var names : [String] = ["why","lmj","lpf"]
    lazy var names : [String] = {
        let names = ["why","lmj","lpf"];
        
        print("-----------")
        
        return names;
    }()
    
    //lazy var btn : UIButton = UIButton();
    
    lazy var btn : UIButton = {
        let btn = UIButton()
        return btn
    }()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("这里是我调用了数据")
        print(names.count)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

上一篇下一篇

猜你喜欢

热点阅读