许多Cocoa和CoCoatTouch方法在Objective-C中以块的形式实现完成回调,在Swift中以闭包的形式实现.然而,当在操场上try 这些时,永远不会要求完成.例如:
// Playground - noun: a place where people can play
import Cocoa
import XCPlayground
let url = NSURL(string: "http://stackoverflow.com")
let request = NSURLRequest(URL: url)
NSURLConnection.sendAsynchronousRequest(request, queue:NSOperationQueue.currentQueue() {
response, maybeData, error in
// This block never gets called?
if let data = maybeData {
let contents = NSString(data:data, encoding:NSUTF8StringEncoding)
println(contents)
} else {
println(error.localizedDescription)
}
}
我可以在我的playground 时间线中看到控制台输出,但我的完成块中的println
从未被调用...