SWIFT 懒加载

谁都会走 提交于 2020-01-14 02:18:09
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // Override point for customization after application launch.
        self.window!.backgroundColor = UIColor.whiteColor()
        
        self.window!.rootViewController = RootViewController()
        
        self.window!.makeKeyAndVisible()
        return true
    }
}
import UIKit

class RootViewController: UIViewController {
    // 懒加载(只调用一次)
    /**
     *  注意:1.必须用var
     *       2.闭包后面必须跟上()
     */
    lazy var data:[String] = {
        ()->[String]
        in
        print("初始化数据")
        return ["123","456","789"]
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        print(data)
        print(data)
        print(data)
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!