Ios swift获取字典的所有值并使用almaofire传递给viewcontroller
我有一个从api收集数据的应用程序,我使用Alamofire和swiftyJSON。我现在面临的挑战是,我在一个数组中有不同的字典,我希望能够返回字典中的特定项。这就是我正在使用的阵列 JsonIos swift获取字典的所有值并使用almaofire传递给viewcontroller,ios,arrays,swift,alamofire,swifty-json,Ios,Arrays,Swift,Alamofire,Swifty Json,我有一个从api收集数据的应用程序,我使用Alamofire和swiftyJSON。我现在面临的挑战是,我在一个数组中有不同的字典,我希望能够返回字典中的特定项。这就是我正在使用的阵列 Json [ { "images": [ { "id": 8, "original": "http://127.0.0.1:8000/media/images/products/2018/05/f3.j
[
{
"images": [
{
"id": 8,
"original": "http://127.0.0.1:8000/media/images/products/2018/05/f3.jpg",
"caption": "",
"display_order": 0,
"date_created": "2018-05-26T17:24:34.762848Z",
"product": 13
},
{
"id": 9,
"original": "http://127.0.0.1:8000/media/images/products/2018/05/f5.jpg",
"caption": "",
"display_order": 1,
"date_created": "2018-05-26T17:24:34.815214Z",
"product": 13
},
{
"id": 10,
"original": "http://127.0.0.1:8000/media/images/products/2018/05/f2.jpg",
"caption": "",
"display_order": 2,
"date_created": "2018-05-26T17:25:19.117271Z",
"product": 13
},
{
"id": 11,
"original": "http://127.0.0.1:8000/media/images/products/2018/05/f4.jpg",
"caption": "",
"display_order": 3,
"date_created": "2018-05-26T17:25:19.155159Z",
"product": 13
}
]
}
]
得到一个单一的图像是这样的
Alamofire.request(URL, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: HEADER).responseJSON { (response) in
if response.result.error == nil {
guard let data = response.data else {return}
do {
if let json = try JSON(data: data).array {
for item in json {
let images = item["images"][0]["original"].stringValue
....
这仅返回索引图像。[0]如果设置为[1],则返回1处的索引图像
如何返回所有图像,以便在集合视图控制器中循环显示所有图像。如有要求,将提供更多代码。您可以这样编辑:
if let json = try? JSON(data: data).arrayValue {
for item in json {
let imagesList = item["images"].arrayValue
let imagesURL = imagesList.map {$0["original"].string}.compactMap({$0})
if imagesURL.count > 0{
print( imagesURL[0])
}
}
}
或:
do {
let json = try JSON(data: data).array
json?.forEach({ (item) in
let imagesList = item["images"].arrayValue
let imagesURL = imagesList.map {$0["original"].string}.compactMap({$0})
if imagesURL.count > 0{
print( imagesURL[0])
}
})
} catch {
print(error.localizedDescription)
}
错误
类型“[String?]”的值没有成员“compactMap”
请确保编写正确的代码让imagesURL=imagesList.map{$0[“original”].String}.compactMap({$0})
如果要删除compactMap,请删除此.compactMap({$0})
我删除了.compactMap({0})它可以工作,但我现在如何从返回的图像数组中选择指定的图像?您需要什么imagesURL
现在是[String?]
String数组