我正在做Ray Wenderlich 的 JSON Parsing in Swift 教程。
使用DataManager.getTopAppsDataFromAppsWithSuccess
in时,在块viewDidLoad()
外无法打印出数组。DataManager
有人知道如何在该块之外获取数组值吗?
代码 :
var apps = [String]()
override func viewDidLoad() {
DataManager.getTopAppsDataFromAppsWithSuccess { (ByodData) -> Void in
let json = JSON(data: ByodData)
if let appArray = json.array{
for appDict in appArray {
var appName: String = appDict["name"].stringValue
self.apps.append(appName)
}
}
println(self.apps)
}
输入 JSON:
[
{
"id":1,
"name":"AdobeReader",
"url":"comp2014group1.herokuapp.com/apps/1.json";
},
{
"id":2,
"name":"BBCiPlayer",
"url":"comp2014group1.herokuapp.com/apps/2.json";
},
{
"id":3,
"name":"BBCNews",
"url":"comp2014group1.herokuapp.com/apps/3.json";
},
{
"id":4,
"name":"BBCWeather",
"url":"comp2014group1.herokuapp.com/apps/4.json";
}
]
输出 :
[]