gpt4 book ai didi

ios - 收到此错误 : nw_protocol_get_quic_image_block_invoke dlopen libquic failed

转载 作者:行者123 更新时间:2023-12-04 04:26:00 36 4
gpt4 key购买 nike

我试图连接我的 API 数据以在单元格中查看它,但似乎我无法得到我的响应,它总是 == nil下面的代码描述了 Country.SWIFT//Model.SWIFT//Response.SWIFT,它显示了如何使用 Codable 获取我的 JSON 响应
CountryCell.SWIFT 正在展示我如何使用它来调用 API
API 图像的链接:
pic
国家.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)

}

}]
型号.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()
}
}
响应.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, response, 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)
}

关于ios - 收到此错误 : nw_protocol_get_quic_image_block_invoke dlopen libquic failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64322845/

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