出现此错误:nw_protocol_get_quic_image_block_invoke dlopen libquic失败

问题描述

我试图连接我的API数据以在单元格中查看它,但看来我听不到我的回应,而且总是== nil 下面的代码描述了Country.SWIFT // Model.SWIFT // Response.SWIFT,它们显示了如何使用Codable获得JSON响应 and CountryCell.SWIFT显示了我如何使用它来调用API API图片链接https://i.stack.imgur.com/jyoON.jpg

Country.SWIFT

struct Country: Decodable {
    
    var CountryName = ""
    var CountryImage = ""
    var objectId = ""


// MARK: - Coding Keys
enum CodingKeys: String,CodingKey {
    case CountryName = "CountryName"
    case CountryImage = "CountryImage"
    case objectId = "objectId"
}

//MARK: - Json Decoder

init(from decoder: Decoder) throws {
    
    let container = try decoder.container(keyedBy: CodingKeys.self)
    // Parsing our attributes
    self.CountryName = try container.decode(String.self,forKey: .CountryName)
    self.CountryImage = try container.decode(String.self,forKey: .CountryImage)
    self.objectId = try container.decode(String.self,forKey: .objectId)

}

}]

Model.SWIFT

protocol ModelDelegate {
    
    func countriesFetched (_ countries: [Country])
    
}

class Model {
    
    //MARK: - Vars
    var delegate: ModelDelegate?
    
    // MARK: - Get Countries
    func getCountries () {
        // URL Object
        let url = URL(string: Constants.API_URL)
        guard url != nil else {return}
        
        // URL Session object
        let session = URLSession.shared
        
        //Data Task from URLSession object
        
        let dataTask = session.dataTask(with: url!) { (data,response,error) in
            
            if error != nil || data == nil {
                print(error!.localizedDescription)
                return
            }
            print(data!)
            
            do {
                let decoder = JSONDecoder()
                let response = try decoder.decode(Response.self,from: data!)
                
                if response.items != nil {
                    
                    dispatchQueue.main.async {
                        self.delegate?.countriesFetched(response.items!)
                    }
                  
                }
            }
            catch {
                
            }
        }
        
        // start data task
        dataTask.resume()
    }
}

Response.SWIFT

 struct Response: Decodable {

    var items: [Country]? = []
    init(from decoder: Decoder) throws {
        var itemsContrainer = try decoder.unkeyedContainer()
        self.items = try itemsContrainer.decode([Country].self)
        
    }
    
    
}

CountryCell.SWIFT

class CountryCell: UICollectionViewCell {
    
    //MARK: - Vars
    var country: Country?
    
    //MARK: - Outlets
    @IBOutlet weak var imageViewCountryOutlet: UIImageView!
    @IBOutlet weak var lblCountryNameOutlet: UILabel!
    
    //MARK: - Creating Cell

    func generateCell (_ myCountry: Country) {
        self.country = myCountry
        
        guard self.country != nil else {  return }
        lblCountryNameOutlet.text = country!.CountryName
        
        guard self.country!.CountryImage != "" else {return}
        
        let url = URL(string: self.country!.CountryImage)
        
        guard url != nil else {return}
  
        let session = URLSession.shared
        
        let dataTask = session.dataTask(with: url!) { (data,error) in
            
            if error == nil || data != nil {
                if url!.absoluteString != self.country!.CountryImage {
                    
                    return
                }

                let image = UIImage(data: data!)

                dispatchQueue.main.async {
                    self.imageViewCountryOutlet.image = image
                    
                }
            }
            
        }

        dataTask.resume()
        
    }
}

解决方法

不需要包装Response类型,直接解码国家列表:

let decoder = JSONDecoder()
let items = try decoder.decode([Country].self,from: data!)

在您的代码中,当您在 Response 中请求 unkeyedContainer 时,预期的 JSON 结构将需要额外的嵌套数组。

检查 catch 块中的解码错误

do {
...
}
catch {
    print(error)
}

,

我知道这听起来有点荒谬,但就我而言,通过使用 class 而不是 struct 并使用 public var 声明所有属性对我有用。