swift 懒加载

作者: 鹏飞说 | 来源:发表于2018-05-02 14:06 被阅读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.
        }
    }
    
    

    相关文章

      网友评论

        本文标题:swift 懒加载

        本文链接:https://www.haomeiwen.com/subject/vyfcrftx.html