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) } }