NSJSONSerialization 在 Playground 中无法按预期工作 [英] NSJSONSerialization not working as expected in a Playground
本文介绍了NSJSONSerialization 在 Playground 中无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须通过 JSON 获取足球比赛时间表,并提取其中一所特定大学的每场比赛的日期.
I have to get football game schedule via JSON and extract date of each game of one of the specific universities.
我试过了:
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 上有合适的网址.
I am trying it in Xcode playground, but it does not print any at print line. And I have appropriate url at SCHOOL URL.
推荐答案
为了在 Xcode Playground 中使用异步操作,您需要将 needsIndefineExecution
设置为 真实代码>.
In order to use asynchronous operations in an Xcode Playground, you need to set needsIndefiniteExecution
to true
.
在代码顶部添加:
Swift 2
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
Swift 3
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
这篇关于NSJSONSerialization 在 Playground 中无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文