gpt4 book ai didi

json - 在 Swift 中从数组中获取项目

转载 作者:搜寻专家 更新时间:2023-11-01 07:06:51 24 4
gpt4 key购买 nike

我是 Swift 的新手,花了几个小时试图从 JSON 响应中提取 photo_url 键。

我用它来读取 JSON:

let jsonDictionary = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)

然后:

                if let eventsDictionary = jsonDictionary {
let upcomingEvents = UpcomingEvents(eventsDictionary: eventsDictionary)
completion(upcomingEvents)
} else {
completion(nil)
}

这是我(失败的)拔出 key 的尝试:

init(eventsDictionary: [String : Any]) {
//photoUrl = eventsDictionary[EventKeys.photoUrl] as? String
let groups: NSArray = eventsDictionary["groups"] as! NSArray
let url: String = groups[0]
print("THIS IS YOUR RETURNED PHOTO URL--\(url)--END OF RETURNED PHOTO URL")
}

enter image description here

我将“[String: Any]”更改为 [String: AnyObject],现在我明白了…… enter image description here

最佳答案

将 Any 转换为 NSArray 时出现问题。只需让您的 Init 方法采用 [String:AnyObject]。但是,这里最好使用 Array 而不是 NSArray

关于json - 在 Swift 中从数组中获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47465229/

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