迅速无法在集合视图中使用api数据 [英] swift unable to use api data in collection View

查看:46
本文介绍了迅速无法在集合视图中使用api数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSON是-

    {
    "status": "success",
    "data": [
        {
            "_id": "15756323",
            "name": "A",
            "icons": "https://sdfgsuew34j.png",
            "createdAt": "2021-03-18T13:06:44.054Z",
            "updatedAt": "2021-03-18T13:06:44.054Z",
            "__v": 0,
            "id": "6053503drm476"
        },
        {
            "_id": "45646054821",
            "name": "S",
            "icons": "https://dkj/djf.png",
            "createdAt": "2021-03-19T10:51:07.066Z",
            "updatedAt": "2021-03-19T10:51:07.066Z",
            "__v": 0,
            "id": "6054821b11kd6873"
        }
]
  }

我正在尝试在collectionView中打印API数据这是一些代码

I'm trying to print API data in collectionView here's some code

    var places: [Post]?
       func apicall() {
            
            let url = URL(string: "http://17.550.457.84/api/category/list")!
            URLSession.shared.dataTask(with: url) { (data, response, error) in
                
                if error == nil {
    
                do {
                    self.places = try JSONDecoder().decode([Post].self, from: data! )
                  
                } catch {
                    print("Error during JSON serialization: \(error.localizedDescription)")
                }
                
            }
        }.resume()
    }

我在调试器中收到一条消息->JSON序列化期间出错:由于数据格式不正确,因此无法读取.

I get a message in the debugger -> Error during JSON serialization: The data couldn’t be read because it isn’t in the correct format.

我也尝试更改-> self.places =尝试JSONDecoder().decode([Post] .self ,从:data!)`到

i also try to change -> self.places = try JSONDecoder().decode([Post].self, from: data! )` to

self.places = try JSONDecoder().decode(Post.self, from: data! )

然后我得到了错误->无法将类型"Post"的值分配为类型"[Post]?"

then I got the error -> Cannot assign the value of type 'Post' to type '[Post]?'

我的模型课在这里

struct Post : Codable {
    let _id : String?
    let name : String?
    let icons : String?
    let createdAt : String?
    let updatedAt : String?
    let __v : Int?
    let id : String?
    
    enum CodingKeys: String, CodingKey {
        
        case _id = "_id"
        case name = "name"
        case icons = "icons"
        case createdAt = "createdAt"
        case updatedAt = "updatedAt"
        case __v = "__v"
        case id = "id"
    }

推荐答案

错误消息基本上说您的模型和即将到来的数据模型(JSON)不相等".您需要像这样更改模型:

The error message basically says "Your model and coming data model(JSON) are not equal". You need to change your model like :

struct BaseModel : Codable{
   var status : String?
   var data : [Post]?
}

当您尝试对其进行反序列化时,您需要创建该BaseModel的实例,

And when you try to deserialize it you need to make a instance of that BaseModel so

    var baseModel: BaseModel?
    var places: [Post]?
 

成功响应时

 do {
       let responseData = try JSONDecoder().decode(BaseModel.self, from: data! )
        self.places = responseData.data!
              
 } catch {              

        print("Error during JSON serialization: \(error.localizedDescription
   }

这篇关于迅速无法在集合视图中使用api数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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