遍历SwiftyJSON [英] Looping through SwiftyJSON

查看:348
本文介绍了遍历SwiftyJSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助.

我无法循环播放imageUrl.它只返回一个图像,而不是遍历所有图像

I can't loop the imageUrl. it only returns one Image rather than iteration through all the images

"images" : [
      {
        "resturantId" : "919db18e-f5e2-4a6b-1df8-08d747e8c08c",
        "id" : "e722f447-a7b8-467c-9b5a-08d747ea79eb",
        "imageUrl" : "https:\/\/149356549.v2.pressablecdn.com\/wp-content\/uploads\/2019\/02\/dominos-pizza.jpg"
      },
      {
        "resturantId" : "919db18e-f5e2-4a6b-1df8-08d747e8c08c",
        "id" : "e6f42a59-f7a1-4a27-9b5b-08d747ea79eb",
        "imageUrl" : "https:\/\/www.pymnts.com\/wp-content\/uploads\/2019\/02\/dominos-earnings-q4-growth.jpg"
      },
      {
        "resturantId" : "919db18e-f5e2-4a6b-1df8-08d747e8c08c",
        "id" : "d10e91eb-8d44-477b-9b5c-08d747ea79eb",
        "imageUrl" : "http:\/\/apparelmagazine.co.nz\/restaurantandcafe\/wp-content\/uploads\/sites\/3\/2019\/06\/DOminos.jpg?w=640"
      }
    ]


func updateRestImage(json : JSON) {

    for for (index, subJson) in json {
        if let restuarantName = subJson["images"][0]["imageUrl"].string {
            print("jnfvjfvjfvubfuvbfuvb yessssss\(restuarantName)")

        }

     }

   restuarantTableView.reloadData()
}

推荐答案

更改图像索引的值

func updateRestImage(json : JSON) {
    let yourRequiredImagesArray = returnJson["images"].arrayValue.compactMap {return Image(data: try! $0.rawData())}

    if !yourRequiredImagesArray.isEmpty{
   restuarantTableView.reloadData()
    }
}

您的模特是

struct Image: Codable {
    let imageUrl
    enum CodingKeys: String, CodingKey {
        case imageUrl
    }
}

extension Image {
    init?(data: Data) {
        guard let img = try? JSONDecoder().decode(Image.self, from: data) else { return nil }
        self = img
    }
}

这篇关于遍历SwiftyJSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆