gpt4 book ai didi

ios - 如何按索引打印 JSON 字典元素?

转载 作者:行者123 更新时间:2023-11-29 01:04:05 26 4
gpt4 key购买 nike

我写了下一个片段:

do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! Dictionary<String, AnyObject>

for i in 0...json.count - 1 {
print(json[i]["title"])
}
} catch let error as NSError {
print(error)
}

在这种情况下我收到错误:

Cannot subscript a value of type 'Dictionary<String, AnyObject>' with an index of type 'Int'

我想通过索引获取元素。我一直使用 Alamofire,但今天我想以默认方式完成所有操作。谁能帮帮我?

最佳答案

你的 json是一个字典 ( Dictionary<String, AnyObject> ) 如果你想遍历一个字典你应该使用下面的语法:

for (someString, someAnyObject) in json {
print(someString, someAnyObject)
}

更多here

关于ios - 如何按索引打印 JSON 字典元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36633203/

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