[转]block(闭包)一点也不神秘—如何利用block进行回调

原文:http://blog.csdn.net/mobanchengshuang/article/details/11751671

原文是OC, 对应 Swift 3 如下:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        print(" === 我在玩手机")
        print(" === 手机没电了")
        self.chargeMyIphone() {
            print(" === 出去逛街了")
        }
        
        //self.perform(#selector(chargeMyIphone), with: nil, afterDelay: 3)
        print(" === 我在看电视")
    }
    
    func chargeMyIphone(completion: @escaping ()->Void) {
        //Thread.sleep(forTimeInterval: 10)
        
        
        let delayInSeconds = 10.0
        let popTime = DispatchTime.now() + delayInSeconds
        DispatchQueue.main.asyncAfter(deadline: popTime, execute: {
            print(" === 电充好了")
            completion()
        })
    }

Comments are closed.