【发布时间】:2014-07-26 08:05:21
【问题描述】:
许多 Cocoa 和 CocoaTouch 方法都有完成回调,在 Objective-C 中实现为块,在 Swift 中实现为闭包。但是,当在 Playground 中尝试这些时,永远不会调用完成。例如:
// 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 从未被调用...
【问题讨论】:
标签: asynchronous callback closures swift swift-playground