3 Replies
      Latest reply on Jan 15, 2020 11:10 PM by Claude31
      ctycnmvt Level 1 Level 1 (0 points)

        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.

        • Re: Best way to organize data
          Claude31 Level 8 Level 8 (7,845 points)

          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

            • Re: Best way to organize data
              ctycnmvt Level 1 Level 1 (0 points)

              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