gpt4 book ai didi

swift - NSJSONSerialization 在 Playground 中没有按预期工作

转载 作者:可可西里 更新时间:2023-11-01 01:37:50 25 4
gpt4 key购买 nike

我必须通过 JSON 获取足球比赛时间表,并提取其中一所特定大学的每场比赛的日期。

我试过:

let url = NSURL(string: "SCHOOL URL")
let request = NSURLRequest(URL: url!)

let session = NSURLSession.sharedSession()

let task = session.dataTaskWithRequest(request){
(data, response, error) -> Void in

do{
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)

if let schedule = jsonData["schedule"] as? [[String: AnyObject]]{
for game in schedule{
if let date = game["date"] as? String{
print("\(date)");
}
}
}

} catch let error as NSError{
print("something bad happened!")
}
}

task.resume()

我正在 Xcode playground 中尝试它,但它不会在打印行打印任何内容。我在 SCHOOL URL 上有适当的 url。

最佳答案

为了在 Xcode Playground 中使用异步操作,您需要将needsIndefiniteExecution 设置为true

在您的代码顶部添加:

swift 2

import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

swift 3

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true

关于swift - NSJSONSerialization 在 Playground 中没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34029442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com