首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Playground中运行异步回调

如何在Playground中运行异步回调
EN

Stack Overflow用户
提问于 2014-06-05 18:55:43
回答 6查看 36.2K关注 0票数 121

许多Cocoa和CocoaTouch方法在Objective-C中将完成回调实现为块,在Swift中将闭包实现为闭包。但是,当在Playground中尝试这些时,从不调用完成。例如:

代码语言:javascript
复制
// 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)
    }
}

我可以在我的游乐场时间线中看到控制台输出,但我的完成块中的println从未被调用过……

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24058336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档