Ios Swift 4:如何获取嵌套Json数据并在CollectionV视图中显示Ui imageView

Ios Swift 4:如何获取嵌套Json数据并在CollectionV视图中显示Ui imageView,ios,json,swift,api,Ios,Json,Swift,Api,我想根据API在collectionViewController的ImageView中获取的路径显示所有图像 我正在使用Codable格式化API获取的Json数据。 我想获取其中的所有嵌套数据(for语句的图像) 现在我试图通过声明let imagePath=store来检索一个值![indexath.row].photos.map{$0.path}但将出现错误无法将类型为“从不”的值转换为预期的参数类型“字符串” StoreViewController import UIKit class

我想根据API在collectionViewController的ImageView中获取的路径显示所有图像

我正在使用
Codable
格式化API获取的Json数据。 我想获取其中的所有嵌套数据(for语句的图像)

现在我试图通过声明
let imagePath=store来检索一个值![indexath.row].photos.map{$0.path}
但将出现错误
无法将类型为“从不”的值转换为预期的参数类型“字符串”

StoreViewController

import UIKit
class StoreViewController: 
UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {
var store_id = ""
var store : [Store]?

@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var mainImage: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var tagLabel: UILabel!
@IBOutlet weak var UIView: UIView!


override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.dataSource = self
    collectionView.delegate = self



    let url = URL(string: "http://localhost:8000/store/api?store_id=" + store_id)
    let request = URLRequest(url: url!)
    let session = URLSession.shared

    let encoder: JSONEncoder = JSONEncoder()
    encoder.dateEncodingStrategy = .iso8601
    encoder.outputFormatting = .prettyPrinted

    session.dataTask(with: request){(data, response, error)in if error == nil,
        let data = data,
        let response = response as? HTTPURLResponse{

        let decoder: JSONDecoder = JSONDecoder()
        decoder.dateDecodingStrategy = .iso8601
        do {

            let json = try decoder.decode(Store.self, from: data)

            //                for store in (json.stores){
            //                    for photo in (store.photos){
            //                        print(photo.path as Any)
            //                    }
            //                }


            self.store = [json]

            DispatchQueue.main.async {
                self.nameLabel.text = json.name
                self.locationLabel.text = json.location
            }

        } catch {
            print("error:", error.localizedDescription)

        }

        }

        }.resume()

}


override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let uiViewPath = UIBezierPath(roundedRect: UIView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 8, height: 8))
    let uiViewMask = CAShapeLayer()
    uiViewMask.path = uiViewPath.cgPath
    UIView.layer.mask = uiViewMask

    //tags
    tagLabel.textAlignment = NSTextAlignment.center
    let tagLabelPath = UIBezierPath(roundedRect: tagLabel.bounds, byRoundingCorners: [.topLeft, .topRight,.bottomLeft,.bottomRight], cornerRadii: CGSize(width: 8, height: 8))
    let tagLabelmask = CAShapeLayer()
    tagLabelmask.path = tagLabelPath.cgPath
    tagLabel.layer.mask = tagLabelmask

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

//Collection
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let imageCell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell",for: indexPath)

    let imageView = imageCell.contentView.viewWithTag(1) as! UIImageView
    //let imageView = UIImageView(frame: CGRect(x:0, y:0, width:imageCell.frame.size.width, height:imageCell.frame.size.height))

    let imagePath = store![indexPath.row].photos.map{$0.path}
    let imageURL = URL(string: "http://127.0.0.1:8000/photos/" + imagePath)
    if imageURL == nil {
        print("nil")
    }else{
        DispatchQueue.global().async {
            let data = try? Data(contentsOf: imageURL!)
            if let data = data {
                let image = UIImage(data: data)
                DispatchQueue.main.async {
                    imageView.image = image
                }
            }
        }
    }

    //print(store)
    print(imagePath)
    return imageCell
}

func collectionView(_ collectionView: UICollectionView,
                    numberOfItemsInSection section: Int) -> Int {

   return store?.count ?? 0
}


}
可编码的

struct Store : Codable {
let id : Int
let name : String
let location : String
let price : String
let open_time : String
let closed_day : String
let categories : [categories]
let tags : [tags]
let photos : [photos]

struct categories : Codable {
    let id : Int
    let name : String
}

struct  tags: Codable {
    let id : Int
    let name : String
}

struct photos : Codable{
    let id : Int
    let path : String
}
}
我的显示图像

尝试使用此代码加载图像i getting image使用此代码但得到相同的错误
无法将类型为“Never”的值转换为预期的参数类型“String”
Oky但在集合视图cellForItemAt方法中从url加载图像时使用了哪种代码加载图像因为每次加载单元格时都要下载,所以加载时间很长这就是为什么我建议你像这样加载它只加载一次,还可以兑现图像。@Alex
let imagePath=store![indexath.row].photos.map{$0.path}
将返回一个字符串数组,而不是一个字符串,这是罪魁祸首。您需要使用
数组。首先
让imagePath=store![indexath.row].photos.map{$0.path}.first