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 |1 Answer
Reset to default 2You 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
resorModel
which should be namedResorModel
will not decode the JSON data you show. You can see thatstart_date
andend_date
are notString
, as declared in your model. Another approach and common practice, is to use a dedicatedlet id = UUID()
andenum CodingKeys: String, CodingKey {...}
without theid
, plenty of examples on SO. – workingdog support Ukraine Commented Mar 21 at 23:43