How to load JSON response with codable structure into tableview using Swift 4.2?

无人久伴 提交于 2021-01-29 08:18:04

问题


My scenario, I am trying to create Codable for my JSON response. I have done codable structure but I don’t know how to load it into tableview. Here, whenever I am selecting multiple tableview cell I need to get all selected cell data username and userid. I don’t know how to start please provide me some suggestion.

My Response

{
    "status": 200,
    "user_list": [
        {
            "user_id": “1234”,
            "permission": 1,
            "active": 1,
            "user": {
                "firstname": “jack”,
                "lastname": “m”
            }
        },etc,...
        ]
}

My codable formate

struct Welcome: Codable {
    let status: Int
    let userList: [UserList]

    enum CodingKeys: String, CodingKey {
        case status
        case userList = "user_list"
    }
}

struct UserList: Codable {
    let userID: String
    let permission, active: Int
    let user: User

    enum CodingKeys: String, CodingKey {
        case userID = "user_id"
        case permission, active, user
    }
}

struct User: Codable {
    let firstname, lastname: String
}

回答1:


First, I would rename your UserList model to User and User just to Name (and rename this property just to name ... then you can rename Name's properties)

struct Welcome: Codable { // you can avoid using `CodingKeys` here since you can
    let status: Int       // set `keyDecodingStrategy` of decoder which does work 
    let userList: [User]  // for you
}

struct User: Codable {
    let userId: String
    let permission, active: Int
    let name: Name

    enum CodingKeys: String, CodingKey {
        case name = "user"
        case permission, active, userId
    }
}

struct Name: Codable {
    let first, last: String
}

Then I think in your case your data source array for UITableView should contains just users, so... your User models.

var users = [User]()

then you can assign this array as your decoded response's userList property

do {
    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
    let decoded = try decoder.decode(Welcome.self, from: someData)
    self.users = decoded.userList
} catch { print(error) }

Now, for UITableView data source method which determine number of rows use count of users

func numberOfSections(in tableView: UITableView) -> Int {
    return users.count
}

and for cellForRowAt use certain user

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    ...
    let user = users[indexPath.row]
    // e.g. let firstname = user.name.first
    ...
}



回答2:


Your API call should return a Data object. To parse it you can use:

 func parseWelcome(dataFromServer:Data,handlerDone: @escaping (_ welcome:Welcome) ->Void) ->Void {
    do {
        let welcome = try JSONDecoder().decode(Welcome.self, from:jsonData!)
        handlerDone(welcome)
    } catch {

        // A handler could be defined here for handeling bad response from server
        print("API:An error occurred while parsing server response Welcome")
    }
}

Call the function and pass your rawData from the server. I conclude you have a User array somewhere, so let's populate it

parseWelcome(dataFromServer: rawData!) { parsedData in

   // Users should be an Array of Users defined by you somewhere
   users = parsedData.userList
}

Call the tableView and load the data from your users array

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        // create a new cell if needed or reuse an old one
        let cell = tableView.dequeueReusableCell(withIdentifier: "notes", for: indexPath)

        // Construct your cell on your needs use users[indexPath.row]
        ...
}

But I agree with @Robert Dresler that you have a bad Model design. It is recommended to redesign it a little bit.



来源:https://stackoverflow.com/questions/54769299/how-to-load-json-response-with-codable-structure-into-tableview-using-swift-4-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!