GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

How should the data model best look like?

Forums > SwiftUI

I am relatively new to SwiftUI and am trying to develop a small app based on what I learned from the iExpense (100days SwiftUI - project 7) project, which stores and lists the expenses for my eCar. I would like to hear the opinion of the experts how I can pack the following data into one or two data models in order to display them.

I have once the loading costs, with the following properties - I have packaged this as a test in structure form to make it clear:

struct LoadingCostItem: Identifiable, Codable, Comparable {

    static func < (lhs: LoadingCostItem, rhs: LoadingCostItem) -> Bool{
            lhs.date > rhs.date  // sorting by date read
                                 // lhs ->  leftHandSite  rhs -> rightHandSite
    }

    var id = UUID()
    var date: Date
    var kwhCharged: Double
    var mileage: Int
    var rangeKm: Int
    var priceKwh: Double
    var priceTotal: Double
}

Then I have the other costs, such as:

struct MiscCostItem: Identifiable, Codable, Comparable {

    static func < (lhs: MiscCostItem, rhs: MiscCostItem) -> Bool{
            lhs.date > rhs.date  // sorting by date read
                                 // lhs ->  leftHandSite  rhs -> rightHandSite
    }

    var id = UUID()
    var date: Date
    var category: String
    var description: String
    var priceTotal: Double
}

My idea is to create two structures/classes, one for charging costs and then for miscellaneous costs. Furthermore I would have two views that show the data in a list and a button that creates/pop-up a sheet to enter the data for the loading costs and miscellaneous costs.

I am thinking back and forth if this is the right solution, or if there is not a clever way that I have only one data model and one view, and then work e.g. via filters in the list. However, the difference in the two above structures seems to me very large, so I do not know how I could/should filter this.

I would be glad about your opinion.

Regards, Ralf

2      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until September 29th.

Click to save your spot

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.