0

After I do http call from api I get json file then I loop through it to get specific values:

   if let value: AnyObject = response.result.value as AnyObject? {
        let json = JSON(value)    
       for (key, subJson) in json {   
           let drivers = subJson["driver"]
           for (key, subJson) in drivers {

              let status = subJson["status"].stringValue
              let date = subJson["created_at"].stringValue
              let name = subJson["name"].stringValue    
              let pivot = subJson["pivot"]
               for (key, subJson) in subJson["pivot"] {
                    let type = pivot["type"].stringValue


                             let data = Driverj(name: name, status: status, created_at: date)


                            self.data.append(data)

                        }


                        DispatchQueue.main.async {
                            self.tableview.reloadData()
                        }
                       }
                     }
            }

in a separate file I connect the values with objects:

class DriverC: UITableViewCell  {

@IBOutlet weak var status : UILabel!
@IBOutlet weak var date : UILabel!
@IBOutlet weak var job : UILabel!
@IBOutlet weak var update : UILabel!
  }

And in another file I have the data saved :

 import Foundation
 import ObjectMapper

  class Driverj : Mappable {


var status : String?
var name : String?
var created_at : String?

required init?(map: Map) {
}
required init(name: String , status : String , created_at : String )  {
    self.status = status
    self.name = name
    self.created_at = created_at



}
func mapping(map: Map) {

    status      <- map["status"]
    name             <- map["name"]
    created_at            <- map["created_at"]
}

}

And finally here is how it's displayed

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "DriverCell", for: indexPath) as! DriverC

    let entry = data[indexPath.row]


    cell.date.text = entry.date
    cell.job.text = entry.job
    cell.status.text = entry.status
    cell.update.text = entry.name
    return cell


}

My question is when I run my project I get three cells with the same data repeated. Why ?

the json files :

  [
{
"id": 4,
"name": null,
"email": "5@5.com",
"status": 0,
"confirmed": false,
"street": "street ",
"niehgborhood": "North",
"city": "Sf",
"national_id": "1009090",
"phone": "9000",
"size_of_house": null,
"created_at": "2016-12-04 13:55:52",
"updated_at": "2017-03-08 14:03:44",
"deleted_at": null,
"driver": [
  {
    "name": "unknown",
    "age": "25",
    "Smoker": "No",
    "language": "English",
    "religion": "Muslim",
    "created_at": null,
    "updated_at": "2017-03-08 13:48:55",
    "status": "تم",
    "pic": "http://localhost:8000/images/1488714520.jpg",
    "id": 1,
    "pivot": {
      "user_id": 4,
      "driver_id": 1,
      "type": "driver"
    }
  },
  {
    "name": "Jae",
    "age": "30",
    "Smoker": "No",
    "language": "English",
    "religion": "Muslim ",
    "created_at": "2017-02-28 09:36:15",
    "updated_at": "2017-03-08 08:46:06",
    "status": "ok",
    "pic": "http://localhost:8000/images/1488714520.jpg",
    "id": 2,
    "pivot": {
      "user_id": 4,
      "driver_id": 2,
      "type": "driver"
AlmoDev
  • 969
  • 2
  • 18
  • 46
  • 1
    How many objects is there in `json`, and how many in `subJson["pivot"]`? Are the objects in `subJson["pivot"]` different drivers you should add in your array? You're doing a loop inside a loop and populating your array from the inner loop. Please debug this step by step inside the loops, look at what are the values you're using to make your driver object. // Also, suggestion: don't name "array" what is actually an instance of a "Driver" object, it's confusing. – Eric Aya Mar 13 '17 at 13:08
  • @EricAya just updated my question can you please look at it – AlmoDev Mar 13 '17 at 13:22
  • 1
    I would recommend using [ObjectMapper](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjG657BttXSAhXBXRQKHTgkAowQFggbMAA&url=https%3A%2F%2Fgithub.com%2FHearst-DD%2FObjectMapper&usg=AFQjCNFyUn25wAdc4YI3J79e10bh5EsWTQ&sig2=dBANlpkBBf7sGgvyAAovag) as mentioned in [this](http://stackoverflow.com/questions/41734982/parsing-nested-array-of-dictionaries-using-object-mapper/41735194#41735194) answer. – Umair Afzal Mar 14 '17 at 07:03
  • @UmairAfzal thank you for introducing me to this great library. So ObjectMapper will replace the model I have in my question to save data ? I think I got the way to use it but I still don't get how to implement it in my situation. – AlmoDev Mar 14 '17 at 07:11
  • Try to follow the example given in linked answer. Then if you find any difficulty ask me. – Umair Afzal Mar 14 '17 at 07:13
  • @UmairAfzal I just followed the example it's still repeating the values three times! – AlmoDev Mar 14 '17 at 09:45
  • Update your question with new implementation. And make sure your data is not duplicated. – Umair Afzal Mar 14 '17 at 09:47
  • @UmairAfzal just did. I didn't add the pivotList because it got more complicated – AlmoDev Mar 14 '17 at 09:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138002/discussion-between-leo0019-and-umair-afzal). – AlmoDev Mar 14 '17 at 10:46
  • Yeah sure lets discuss in chat – Umair Afzal Mar 14 '17 at 12:00

0 Answers0