UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.