Swift 不解码JSON响应以填充集合视图
我试图用从远程PHP文件接收的数据填充集合视图 远程PHP文件响应如下所示:Swift 不解码JSON响应以填充集合视图,swift,uicollectionview,Swift,Uicollectionview,我试图用从远程PHP文件接收的数据填充集合视图 远程PHP文件响应如下所示: [{"id":12,"nombre":"AGM","icono":"vCtAi1agmlogo.png"},{"id":10,"nombre":"Acer","icono":"h4QnIZacerlogo.png"},{"id":9,"nombre":"Apple","icono":"byjwZDapplelogo.png"},{"id":55,"nombre":"Cubot","icono":"xWQnfbcubot
[{"id":12,"nombre":"AGM","icono":"vCtAi1agmlogo.png"},{"id":10,"nombre":"Acer","icono":"h4QnIZacerlogo.png"},{"id":9,"nombre":"Apple","icono":"byjwZDapplelogo.png"},{"id":55,"nombre":"Cubot","icono":"xWQnfbcubotlogo.png"},{"id":110,"nombre":"Huawei","icono":"GYacqrhuaweilogo.png"},{"id":121,"nombre":"Innjoo","icono":"yWP39Ninnjoologo.png"},{"id":151,"nombre":"Lenovo","icono":"UL54jTlenovologo.png"},{"id":158,"nombre":"Majestic","icono":"ayDxNAmajesticlogo.png"},{"id":204,"nombre":"Polar","icono":"SQ3LXHpolarlogo.png"},{"id":218,"nombre":"Samsung","icono":"Vrs4hpSamsunglogo.jpg"},{"id":229,"nombre":"Sony","icono":"ZyFcI2sonylogo.png"},{"id":275,"nombre":"Xiaomi","icono":"5kRBCoxiaomilogo.png"}]
这里有集合视图的所有方法:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return marcas.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("MARCA:"+marcas[indexPath.row].nombre)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "marca",for: indexPath) as! MarcasCollectionViewCell
let foto_marca = marcas[indexPath.row].icono
print(foto_marca)
let url = URL(string: "https://../iconos/"+foto_marca)
UIImage.loadFrom(url: url!) { image in
cell.icono_marca.image = image
}
return cell
}
这是我用来获取数据的下载功能:
func downloadJSON(completed: @escaping () -> ()) {
let url = URL(string: "https://.../Api.php")
URLSession.shared.dataTask(with: url!) { (data,response,error) in
print("Data:",data as Any)
print("Response:",response as Any)
print("Error:",error as Any)
if error == nil {
do {
self.marcas = try JSONDecoder().decode([Marcas].self, from: data!)
DispatchQueue.main.async {
completed()
}
}catch{
print("JSON Error")
}
}
}.resume()
}
这是Marcas的模型:
struct Marcas:Decodable {
let id: String
let nombre: String
let icono: String
}
正如您可能看到的,我已经包含了三个打印标签来获取服务器的响应
这里有每个标签的打印输出:
Data: Optional(702 bytes)
Response: Optional(<NSHTTPURLResponse: 0x600000874be0> { URL: https://jogua.es/android_api/Api.php } { Status Code: 200, Headers {
"Content-Encoding" = (
br
);
"Content-Type" = (
"text/html; charset=UTF-8"
);
Date = (
"Fri, 20 Mar 2020 17:38:10 GMT"
);
Server = (
cloudflare
);
Vary = (
"Accept-Encoding"
);
"cf-cache-status" = (
DYNAMIC
);
"cf-ray" = (
"5771348e09c868f4-CDG"
);
"expect-ct" = (
"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""
);
} })
Error: nil
数据:可选(702字节)
响应:可选({URL:https://jogua.es/android_api/Api.php }{状态代码:200,标题{
“内容编码”=(
溴
);
“内容类型”=(
“text/html;字符集=UTF-8”
);
日期=(
“2020年3月20日星期五17:38:10 GMT”
);
服务器=(
云焰
);
变化=(
“接受编码”
);
“cf缓存状态”=(
动态
);
“cf射线”=(
“5771348e09c868f4 CDG”
);
“期望ct”=(
“最大年龄=604800,报告uri=\”https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""
);
} })
错误:无
最后,我得到了JSON错误输出
如何获取集合视图的项目?将模型更改为:
struct Marcas:Decodable {
let id: Int
let nombre, icono: String
}
不要打印无意义的文字字符串
“JSON错误”
。打印错误
实例。它将告诉您id
的类型不是字符串。可能存在两个错误。一个来自数据任务,一个来自JSONDecoder。我指的是后者。将print(“JSON Error”)
替换为print(Error)
这是我在第一条评论中说的:id
的类型不是字符串。,这是Int
您的JSON
正在为键id
发送Int
值,而不是String
,这就是它无法解析的原因。请学习阅读DecodingError
消息。它们非常全面。谢谢,我在打印错误后得到了相同的答案。