gpt4 book ai didi

arrays - 如何在本地数组 swift 3 中保存来自 Alamofire 的 response.result.value

转载 作者:行者123 更新时间:2023-11-28 10:55:45 24 4
gpt4 key购买 nike

Alamofire.request(NEWS_FEED_URL).responseJSON { response in

guard let newsResponse = response.result.value else{
print("Error: \(response.result.error)")
return
}
print("JSON: \(newsResponse)")
let localArray:Array = [newsResponse]
print("Local Array:", localArray)
}



I am using alamofire to fetch data from server and trying to write parser on the response data. When I fetch response.result.value from alamofire and print it is working fine. But as soon as I put response.result.value in my local array it gives


[<__NSArrayI 0x6080000bcf80>(
{
category = World;
})]

以此类推(控制台)。此外,我无法将“Any”类型的响应值转换为“Array”。请帮助我如何在“Array”类型的本地数组中添加 response.result.value。有人可以简要解释一下在 alamofire 上进行类型转换吗?

最佳答案

您只需将 response.result.value 的结果转换为字典数组。

guard let newsResponse = response.result.value as? [[String:String]] else{
print("Error: \(response.result.error)")
return
}
print("JSON: \(newsResponse)")
let localArray = newsResponse
//Access the localArray
print("Local Array:", localArray)
//Now to get the desired value from dictionary try like this way.
let dic = localArray[0]
let title = dic["title"] ?? "Default Title"
let link = dic["link"] as? "Default link"
//... get other value same way.

注意:您的响应已经是一个数组,所以不要通过将它添加到另一个数组中来从中创建数组。

关于arrays - 如何在本地数组 swift 3 中保存来自 Alamofire 的 response.result.value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43781119/

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