Swift 懶載入

來源:互聯網
上載者:User

標籤:orm   啟動   code   names   copyright   lazy   res   for   over   

////  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 懶載入

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.