Best way to organize data

Hey all,


Apologies for the intro-level question.


What is the best way to structure and save lots of information in a heirarchy, ex: storing lots of car models which are all cars, and lots of bike models which are all bikes? Each car would have a many properties (make, model, weight, speed, wheel size, etc), and there will be many cars. Eventually I want to show a UITableView of car names and when a user taps on a name a modal is presented that shows more data.


Even quick terms to google would be a huge help.

Accepted Reply

Do you mean structure in Swift, or in CoreData ?


For Swift, I would do this:


You may need some enum with associated values, such as:

enum ChargeMode: String {
     case ac: "ac"
     case dc: "dc"
     case dcFast: "dcFast"
}


enum Fuel : String {
     case petrol: "petrol"
     case gas: "gas"
     case h2: "hydrogen"
     case lpg: "lpg"
     // and so on
}


enum Energy {
     case gazoline(fiuel: Fuel, tank: Int, mpg: Float)
     case electric(battery: Int, mpk: Float, chargeMode:ChargeMode, chargeSpeed: Int )
}


Create a class Vehicle, with common properties:

class Vehicle {
     var make: String
     var model: String
     var wheels: Int
     var energy: Energy
     var weight: Int
     var speed: Int
     // and so on
}


then you could subclass

class PassengerCar: Vehicle {
     var numlberOfpassengers: Int
     var trunkVolume: Int
}


Is it what you were looking for ?


In CoreData, you would organize with Entities, attributes and relations

Replies

Do you mean structure in Swift, or in CoreData ?


For Swift, I would do this:


You may need some enum with associated values, such as:

enum ChargeMode: String {
     case ac: "ac"
     case dc: "dc"
     case dcFast: "dcFast"
}


enum Fuel : String {
     case petrol: "petrol"
     case gas: "gas"
     case h2: "hydrogen"
     case lpg: "lpg"
     // and so on
}


enum Energy {
     case gazoline(fiuel: Fuel, tank: Int, mpg: Float)
     case electric(battery: Int, mpk: Float, chargeMode:ChargeMode, chargeSpeed: Int )
}


Create a class Vehicle, with common properties:

class Vehicle {
     var make: String
     var model: String
     var wheels: Int
     var energy: Energy
     var weight: Int
     var speed: Int
     // and so on
}


then you could subclass

class PassengerCar: Vehicle {
     var numlberOfpassengers: Int
     var trunkVolume: Int
}


Is it what you were looking for ?


In CoreData, you would organize with Entities, attributes and relations

I believe this is what I'm looking for. Is it poor practice to have ~40 subclasses stuffed in one swift file? It almost feels wrong to use classes in this way, thought perhaps I just need to do more reading on swift classes. I'm still quite new.


Thanks a ton for the answer though, marked as correct 🙂

Yes, don't create 40 different classes.


You should do it probably with just a few.

Use of associated values (look in Swift handbook) should help achieve thos, as shown in my example.


Good luck.