Hi @Claude31 I will start two other threat as I'm progressing thru my app thansk to all your learning and idea.
I have spotted this id: String in TimekeyinList was useless during my cleaning,
I actually also spot that if I put your code into a LIST then the code is not working anymore strange behaviour, so I remove the list{} and use. a VStack instead as per your recaumentdation Spacing Vstack if what I needed.
As for how I parse my JSON it's exactly as per your link ,
here is the way I parse :```
//
// ModelData.swift
// MyPmV1
//
// Created by Sebastien BENAVIDES on 3/2/24.
//
import Foundation
import Observation
@Observable
class ModelData {
var projects: [Project] = load("ProjectData.json")
var hikes: [Hike] = load("hikeData.json")
var profile = Profile.default
var features: [Project] {
projects.filter { $0.isFeatured }
}
var categories: [String: [Project]] {
Dictionary(
grouping: projects,
by: { $0.category.rawValue }
)
}
var buisness: [String: [Project]] {
Dictionary(
grouping: projects,
by: { $0.buisness_model.rawValue }
)
}
var customers: [String: [Project]] {
Dictionary(
grouping: projects,
by: { $0.customer.rawValue }
)
}
}
func load<T: Decodable>(_ filename: String) -> T {
let data: Data
guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
else {
fatalError("Couldn't find \(filename) in main bundle.")
}
do {
data = try Data(contentsOf: file)
} catch {
fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
}
do {
let decoder = JSONDecoder()
return try decoder.decode(T.self, from: data)
} catch {
fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
}
}
``
I try to use UUID as per your recommendation and the mentioned of the. link below but code keys is way to much in term of string as to be decodable I need to use all the variables inside the project..... which is way to much and some of them are GPS coordinates.
here is a glimpse of one project in my JSON
"name": "HOLIDAY",
"shortname": "HOLIDAY",
"leftPMtime": 999,
"category": "Food & Baverage",
"city": "TBD",
"country": "Singapore",
"buisness_model": "BM3",
"state": "TBD",
"id": 1014,
"isSpecific": false,
"isFeatured": false,
"isFavorite": false,
"isTeco": false,
"customer": "Internal",
"coordinates": {
"longitude": 106.8451,
"latitude": -6.2146
},
"description": "To be written",
"imageName": "TPLogo"
and here is a glimpse of my Projects.swift file
import Foundation
import SwiftUI
import CoreLocation
struct Project: Hashable, Codable,Identifiable {
var id: Int
var name: String
var shortname: String
var leftPMtime: Int
// var city: String
// var country: String
var state: String
var description: String
var isFeatured: Bool
var isSpecific: Bool
var isFavorite: Bool
var isTeco: Bool
var category: Category
enum Category: String, CaseIterable, Codable {
case foodAndBaverage = "Food & Baverage"
case NutritionAndBioscience = "Nutrition & Bioscience"
}
var buisness_model: Buisness
enum Buisness: String, CaseIterable, Codable {
case BM1 = "BM1"
case BM2 = "BM2"
case BM3 = "BM3"
}
var customer: CustomerCat
enum CustomerCat: String, CaseIterable, Codable {
case Nestle = "Nestle"
case IFF = "IFF"
case FFI = "FFI"
case IFFCO = "IFFCO"
case Abbott = "Abbott"
case Mondelez = "Mondelez"
case Internal = "Internal"
}
private var imageName: String
var image: Image {
Image(imageName)
}
var featureImage: Image? {
isFeatured ? Image(imageName + "_feature") : nil
}
private var coordinates: Coordinates
var locationCoordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(
latitude: coordinates.latitude,
longitude: coordinates.longitude)
}
struct Coordinates: Hashable, Codable {
var latitude: Double
var longitude: Double
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: