UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Type 'JSONLocationData' does not conform to protocol 'Identifiable' ??

Forums > SwiftUI

My JSON Can someone please tell me what I am doing wrong please.

{
   "locations": [
      {
         "keyword": "a",
         "location_id": "453",
         "story_id": "172",
         "event": "Floyd Lippencotte Interviews"
      },
      {
         "keyword": "A",
         "location_id": "220",
         "story_id": "141",
         "event": "HCRI"
      }
   ]
}
struct LocationsResponse: Decodable {
  let locations: [JSONLocationData]
}

// My Error says  "Type 'JSONLocationData' does not conform to protocol 'Identifiable'"
struct JSONLocationData : Identifiable, Decodable, Hashable {  

    let location_id : String

    var event : String

    var keyword: String

    var story_id: String
}

3      

Anything that conforms to Identifiable is required to have a property called id.

You can easily satisfy this requirement with a computed property that just returns the location_id, if that is the identifying bit of info:

var id: String { location_id }

3      

First of all, THANK YOU FOR REPLYING. I deeply appriciate the help in this. Once I grasp this mystery, I will be inputting it straing into CoreData.

And Yes: var id: String { location_id } is what is esential, since my List depends on listing everything that my List needs to populate to. My Error They way it is layed out, I can not grasp it


// Model...
struct LocationsResponse: Decodable {
  let locations: [JSONLocationData]
}

struct JSONLocationData : Identifiable, Decodable, Hashable {

    var id: String { location_id }

    var event : String

    var keyword: String

    var story_id: String
}

// completion Handler For JSON Data...

// returning array of user data ..

func getLocationData(url: String,completion : @escaping ([JSONLocationData])->()) {
    let session = URLSession(configuration: .default)
    session.dataTask(with: URL(string: url)!) { (data, _, err) in
        if err != nil {
            print(err!.localizedDescription)
            return
        }
        // decoding JSON Location...

        do {
            let location = try JSONDecoder().decode(LocationsResponse.self, from: data!)
            completion(location.locations)

        }
        catch {
            print(error)
        }
    }
    .resume()
}

3      

You still need the location_id property as decoded from your JSON. The computed id property I suggested is in addition to the existing location_id. Because id has to get location_id from somewhere in order to return it.

3      

First of all, THANK YOU FOR REPLYING.

And Yes: var id: String { location_id } is what is esential, since my List depends on listing everything that my List needs to populate to. I am still trying to understand my problem. But I will try and figure it out, I WILL GET BACK AND LETTING YOU KNOW. I will try my best to figuring it out with the information that you have given me as my task to learn. Why I am not grasping it, I do not know. What fustrates me the most is that, with Objective C, I had such an easier time to grasp. And that I could do incredible complex coding with Objective C. Why I am having such a difficulties with a structure that suppose to be easier. This is the most fustrating thing for to accept. THANK YOU AGAIN and I will report back on my progress. Sincerely, Robert

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.