Skip to content

Instantly share code, notes, and snippets.

@gradyzhuo
Created January 27, 2016 03:35
Show Gist options
  • Save gradyzhuo/f3346262f3073f224ee3 to your computer and use it in GitHub Desktop.
Save gradyzhuo/f3346262f3073f224ee3 to your computer and use it in GitHub Desktop.
func doAsyncAction(completionHandler:(callback:AnyObject)->Void){
let delay = Int64(NSEC_PER_MSEC*100)
let afterTime = dispatch_time(DISPATCH_TIME_NOW, delay)
let queue = dispatch_get_global_queue(0, 0)
//等待100毫秒(0.1秒)後再執行
dispatch_after(afterTime, queue) { () -> Void in
completionHandler(callback: "Hello world")
}
}
/*以上先不用看,先假設有一個程序 doAsyncAction 會delay 100毫秒(0.1秒)後才會執行*/
print("1")
//執行doAsyncAction
doAsyncAction { (callback) -> Void in
print("2")
print("callback:\(callback)")
print("3")
}
print("4")
/* 印出來的順序還會是 1, 2, 3, 4嗎?*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment