json - How can I conform to Identifiable without adding an extra id? - Stack Overflow

So I have a JSON-file, a model as a struct, a class with a function todecode the JSON and a view with

So I have a JSON-file, a model as a struct, a class with a function to

decode the JSON and a view with a list to show the contents of the JSON.

I have tried to follow a course but the code is my own.

I might have missunderstood something.

I think my problem is that I need an id-property so my model conforms to Identifiable.

There was something about computing a property. Even if I understand that that the property is computed every time I don't understand how it is suppose to work here.

When I run the preview it shows nothing.

[
  {
   "service_id": 1,
    "monday": 0,
    "tuesday": 0,
    "wednesday": 0,
    "thursday": 0,
    "friday": 0,
    "saturday": 0,
    "sunday": 0,
    "start_date": 20250127,
    "end_date": 20250619
  },
  {
    "service_id": 2,
    "monday": 0,
    "tuesday": 0,
    "wednesday": 0,
    "thursday": 0,
    "friday": 0,
    "saturday": 0,
    "sunday": 0,
    "start_date": 20250125,
    "end_date": 20250614
  },
  {
    "service_id": 3,
    "monday": 0,
    "tuesday": 0,
    "wednesday": 0,
    "thursday": 0,
    "friday": 0,
    "saturday": 0,
    "sunday": 0,
    "start_date": 20250126,
    "end_date": 20250615
  },
  {
    "service_id": 4,
    "monday": 0,
    "tuesday": 0,
    "wednesday": 0,
    "thursday": 0,
    "friday": 0,
    "saturday": 0,
    "sunday": 0,
    "start_date": 20250125,
    "end_date": 20250615
  }
]
struct resorModel: Decodable, Identifiable {   //Convert JSON to a Swift type
    let id: Int
    let serviceId: Int
    let monday: Int
    let tuesday: Int
    let wednesday: Int
    let thursday: Int
    let friday: Int
    let saturday: Int
    let sunday: Int
    let startDate: String
    let endDate: String
    
}

/This file is Controller in MVC
//Between data (json) and view.

import Foundation //Needed for decoding

class Resor {
    var diffentStops: [resorModel] = [] 
    
    
    init() { //When a new instance is created
        decodeResorData()  //Runs the function below
    }
    
    func decodeResorData(){
        if let url = Bundle.main.url(forResource: "csvjson", withExtension: "json"){ 
            do {
                let data = try Data(contentsOf: url)
                let decoder = JSONDecoder()
                decoder.keyDecodingStrategy = .convertFromSnakeCase
                diffentStops = try decoder.decode([resorModel].self, from: data) 
            } catch {
                
                print("Error decoding JSON data: \(error)")
            }
        }//End if..
    }//End func
}//End class

import SwiftUI

struct showStops: View {
    
    let allStops = Resor() //Create an objects that is filled with stopps,
                        
    
    var body: some View {
        List(allStops.diffentStops) { enResa in
            Text(enResa.startDate) // description omvandlar till string
        }
    }
}

#Preview {
    showStops()
}

I tried to print the startdate but nothing shows.

So I have a JSON-file, a model as a struct, a class with a function to

decode the JSON and a view with a list to show the contents of the JSON.

I have tried to follow a course but the code is my own.

I might have missunderstood something.

I think my problem is that I need an id-property so my model conforms to Identifiable.

There was something about computing a property. Even if I understand that that the property is computed every time I don't understand how it is suppose to work here.

When I run the preview it shows nothing.

[
  {
   "service_id": 1,
    "monday": 0,
    "tuesday": 0,
    "wednesday": 0,
    "thursday": 0,
    "friday": 0,
    "saturday": 0,
    "sunday": 0,
    "start_date": 20250127,
    "end_date": 20250619
  },
  {
    "service_id": 2,
    "monday": 0,
    "tuesday": 0,
    "wednesday": 0,
    "thursday": 0,
    "friday": 0,
    "saturday": 0,
    "sunday": 0,
    "start_date": 20250125,
    "end_date": 20250614
  },
  {
    "service_id": 3,
    "monday": 0,
    "tuesday": 0,
    "wednesday": 0,
    "thursday": 0,
    "friday": 0,
    "saturday": 0,
    "sunday": 0,
    "start_date": 20250126,
    "end_date": 20250615
  },
  {
    "service_id": 4,
    "monday": 0,
    "tuesday": 0,
    "wednesday": 0,
    "thursday": 0,
    "friday": 0,
    "saturday": 0,
    "sunday": 0,
    "start_date": 20250125,
    "end_date": 20250615
  }
]
struct resorModel: Decodable, Identifiable {   //Convert JSON to a Swift type
    let id: Int
    let serviceId: Int
    let monday: Int
    let tuesday: Int
    let wednesday: Int
    let thursday: Int
    let friday: Int
    let saturday: Int
    let sunday: Int
    let startDate: String
    let endDate: String
    
}

/This file is Controller in MVC
//Between data (json) and view.

import Foundation //Needed for decoding

class Resor {
    var diffentStops: [resorModel] = [] 
    
    
    init() { //When a new instance is created
        decodeResorData()  //Runs the function below
    }
    
    func decodeResorData(){
        if let url = Bundle.main.url(forResource: "csvjson", withExtension: "json"){ 
            do {
                let data = try Data(contentsOf: url)
                let decoder = JSONDecoder()
                decoder.keyDecodingStrategy = .convertFromSnakeCase
                diffentStops = try decoder.decode([resorModel].self, from: data) 
            } catch {
                
                print("Error decoding JSON data: \(error)")
            }
        }//End if..
    }//End func
}//End class

import SwiftUI

struct showStops: View {
    
    let allStops = Resor() //Create an objects that is filled with stopps,
                        
    
    var body: some View {
        List(allStops.diffentStops) { enResa in
            Text(enResa.startDate) // description omvandlar till string
        }
    }
}

#Preview {
    showStops()
}

I tried to print the startdate but nothing shows.

Share Improve this question asked Mar 21 at 20:48 Lars PerssonLars Persson 11 silver badge1 bronze badge 1
  • Note, your resorModel which should be named ResorModel will not decode the JSON data you show. You can see that start_date and end_date are not String, as declared in your model. Another approach and common practice, is to use a dedicated let id = UUID() and enum CodingKeys: String, CodingKey {...} without the id, plenty of examples on SO. – workingdog support Ukraine Commented Mar 21 at 23:43
Add a comment  | 

1 Answer 1

Reset to default 2

You can add it as a computed property that uses anything you already have that is unique.

struct resorModel: Decodable, Identifiable {
    var id: Int {
        serviceId
    }
    let serviceId: Int
    let monday: Int
    let tuesday: Int
    let wednesday: Int
    let thursday: Int
    let friday: Int
    let saturday: Int
    let sunday: Int
    let startDate: Date
    let endDate: Date
    
}

You can also tell List what is unique

List(allStops.diffentStops, id:\.serviceId) { enResa in

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744333933a4569020.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信