gpt4 book ai didi

swift - 组合多个 NSMutableDictionary

转载 作者:行者123 更新时间:2023-11-30 13:43:10 28 4
gpt4 key购买 nike

我有一个递归函数,可以将以前的字典添加到新字典中,但使用 addEntriesFromDictionary 会出现错误

这是我的示例代码(已编辑)

    func fetchAllSWApiList(strUrl: String, strArrayCallback: NSMutableDictionary){
let swApiHandler = SwApiHandler()
let url = strUrl

swApiHandler.requestSWPApi(url, completionHandler: {(response, error) in
if let responseDictionary = response as? Dictionary<String, AnyObject> {
print("Did Detect a dictionary")
strArrayCallback.addEntriesFromDictionary(responseDictionary)
}
guard let nextPage: String = response!["next"]! as? String else {
print("End Of Page")
print(strArrayCallback.description)
return
}

print(nextPage)
self.fetchAllSWApiList(nextPage, strArrayCallback: strArrayCallback)

})

}

如果您在我的代码中有更好的解决方案,可以递归地从获取的数据中返回组合列表,那就更好了。

编辑:这是错误

NSDictionary is not implicitly convertible to '[NSObject: AnyObject]'; did you mean to use 'as' to explicitly convert?

之后我的代码将变成我应该保持原样还是编辑 NSpbject 部分?

strArrayCallback.addEntriesFromDictionary(response! as [NSObject : AnyObject])

编辑2:如果您想知道 JSON 模型是什么样子,这是它的图片

enter image description here

最佳答案

您应该将响应转换为您期望的字典类型,方法如下:

if let responseDictionary = response as? Dictionary<String, AnyObject> {
strArrayCallback.addEntriesFromDictionary(responseDictionary)
}

关于swift - 组合多个 NSMutableDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35306646/

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