多くの Cocoa および CocoaTouch メソッドには、Objective-C ではブロックとして、Swift では Closures として実装された完了コールバックがあります。ただし、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
、完了ブロックでは呼び出されません...