许多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在我的完成块中从未调用过…
println
XCPlayground
true`。如果设置了此属性,则在您的顶级游乐场源完成时,我们将继续旋转主运行循环,而不是在那里停止游乐场,因此异步代码有机会运行。我们最终将在默认为30秒的超时后终止操场,但是如果您打开助手编辑器并显示时间轴助手,则可以对其进行配置。超时在右下角。
例如,在Swift 3中(使用URLSession代替NSURLConnection):
URLSession
NSURLConnection
import UIKit import PlaygroundSupport let url = URL(string: "http://stackoverflow.com")! URLSession.shared.dataTask(with: url) { data, response, error in guard let data = data, error == nil else { print(error ?? "Unknown error") return } let contents = String(data: data, encoding: .utf8) print(contents!) }.resume() PlaygroundPage.current.needsIndefiniteExecution = true
或在Swift 2中:
import UIKit import XCPlayground let url = NSURL(string: "http://stackoverflow.com") let request = NSURLRequest(URL: url!) NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.currentQueue()) { response, maybeData, error in if let data = maybeData { let contents = NSString(data:data, encoding:NSUTF8StringEncoding) println(contents) } else { println(error.localizedDescription) } } XCPlaygroundPage.currentPage.needsIndefiniteExecution = true