代码改变世界

SWIFT 闭包的简单使用

2016-02-21 18:57  甘雨路  阅读(215)  评论(0编辑  收藏  举报
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 {

    override func viewDidLoad() {
        super.viewDidLoad()
        //调用processData函数
        processData { () -> () in
            print("被回调了");
        }
    }
    
    func processData(printSomething:()->()){
        print("执行操作");
        //执行闭包回调
        printSomething();
    }

}