Saturday, November 7, 2026
HomeiOS Developmentios - ObjectMapper: Mapping JSON worth that depends upon an different class

ios – ObjectMapper: Mapping JSON worth that depends upon an different class

[ad_1]

I have been engaged on just a little mission and figured I wish to load all my knowledge by way of JSON and never hard-code the (mock) objects in my courses.

So I seemed round and located this ObjectMapper and determined to make use of it. I’ve the next structs that kind my knowledge mannequin:

PointOfInterest:

struct PointOfInterest: Identifiable {
    
    let id = UUID()
    
    var identify: String
    
    var class: Class
    
    var likes: Int = 0
    
    var dateOfNotification: Date?
    var notificationTriggered: Bool = false
    
    personal var coordinates: Coordinates
    var location: CLLocation {
        CLLocation(latitude: coordinates.latitude, longitude: coordinates.longitude)
    }
    
}

Class:

class Class: Identifiable {
    
    var id = UUID()
    
    var identify: String
    
    var uiImage: UIImage
    var icon: Picture {
        Picture(uiImage: uiImage)
    }
    
}

I’ve the next class that holds all of the initialized objects:

ultimate class ModelData: ObservableObject {
    
    @Revealed var classes: [Category] = Mapper<Class>().mapArray(JSONfile: "classes.json") ?? []
    
    @Revealed var pointsOfInterest: [PointOfInterest] = Mapper<PointOfInterest>().mapArray(JSONfile: "pointOfInterest.json") ?? []
    
}

Now I’m struggling to know how I can init the ‘class’ property on my PointOfInterest struct as a result of I wish to load each struct from respective JSON information. Clearly there are just a few classes and I want to reuse these classes for various PointsOfInterest-Objects.

I considered placing an index or Id of the class within the respective subject contained in the json and assign the right class on load. However I’m not certain how this works or whether it is even potential.

Can somebody assist me? Thanks 🙂

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments