2
\$\begingroup\$

I'm trying to "build" a few "spaceships" in my code. I'm feeling it's a good idea to enumerate all the spaceships and use that as a subscript to build and use an array that holds the spaceships' properties. After researching Stack Overflow and other places, I found a solution that works, but I'm feeling it's not very elegant and I'm probably not making good use of the Swift language. I'm also unhappy with having to define the name (string) of the spaceship again (and not using the enum case for that).

Do I miss the obvious cleaner solution? Any suggestions for a better solution?

enum SpaceshipName: Int, CaseIterable {
  case Orion
  case Vega
  case Gemini
}

struct SpaceshipProperties {
  var name: String = "TBD"
  var fuel: Int = 0
  var speed: Int = 0
}


// Initialize the spaceships:

var spaceShips = [SpaceshipProperties]()

for s in SpaceshipName.allCases {
  spaceShips.append(SpaceshipProperties())
}

// Now start using the spaceships:

spaceShips[SpaceshipName.Orion.rawValue].name = "Orion"
spaceShips[SpaceshipName.Orion.rawValue].fuel = 100
spaceShips[SpaceshipName.Gemini.rawValue].speed = 2200
// ... and so forth

// Ultimately, I want to iterate through the spaceships in the following manner:

for s in SpaceshipName.allCases {
  // Perform calculations with all the spaceships here, for example:
  let newSpeed = spaceShips[s.rawValue].speed + 10
  spaceShips[s.rawValue].speed = newSpeed
}
```
\$\endgroup\$
0
\$\begingroup\$

Maybe you should create enum with property? Something like this, I don't know if it is what you want but hope it will be helpful.

enum Ship {

    case first(ShipPropperties), second(ShipPropperties), third(ShipPropperties)

    static let allValues: [Ship] = [first(ShipPropperties()), second(ShipPropperties()), third(ShipPropperties())]

    var properties: ShipPropperties {
        switch self {
        case .first(let properties): return properties
        case .second(let properties): return properties
        case .third(let properties): return properties
        }
    }

    func changeProperties(to: ShipPropperties) {
        properties.changeProperties(name: to.name, fuel: to.fuel, speed: to.speed)
    }

}

class ShipPropperties {

    var name: String = "TBD"
    var fuel: Int = 0
    var speed: Int = 0

    func changeProperties(name: String, fuel: Int, speed: Int) {
        self.name = name
        self.fuel = fuel
        self.speed = speed
    }

}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ thank you. That's a promising idea to print the properties into the enum. With my limited Swift knowledge, however, I fail to see how I would access (read or write) the actual properties. Do I understand correctly that, with the above code, I would be limited to writing all the properties per ship at once? \$\endgroup\$ – Adrian Nov 30 '19 at 13:18
  • \$\begingroup\$ no, you can set properties using for loop in Ship.allValues \$\endgroup\$ – Alexandr Kolesnik Dec 2 '19 at 7:15
0
\$\begingroup\$

SpaceShipName enum will keep your ships. In swift, enums can have a function in that case that is matchspaces. With using this function you can pass your struct data which is consist of spaceShipProperties like a fuel,speed. I demonstrate to you how to use it, after that, you just need to create an array, then inside of for loop print each of spaceShip's members easily

// for use 

SpaceShipName.Orion.matchSpaces() // return spaceShip struct object 


struct SpaceshipProperties {
    var name: String = "TBD"
    var fuel: Int = 0
    var speed: Int = 0
}

enum SpaceShipName: String  {

    case Orion = "Orion"
    case Vega =  "Vega"
    case Gemini = "Gemini"

    func matchSpaces()  -> SpaceShip {

        var  spaceShip: SpaceShip

        switch self {

        case .Orion:
            spaceShip   =  SpaceShipProporties('name', 'yourFuel','200')

        case .Vega:
            spaceShip   =  SpaceShipProporties('name2', 'yourFuel','150')

        case .Gemini:
            spaceShip   =  SpaceShipProporties('names', 'yourFuel','175')
        }


        return spaceShip
    }
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ thank you. If I understand your code correctly, it wouldn't allow me to write the spaceship properties (in the SpaceshipProperties struct), just read them, right? \$\endgroup\$ – Adrian Nov 29 '19 at 14:16
  • \$\begingroup\$ yeah just read them. İnside of matchSpaces() function you create SpaceShipProporties struct . After than call it, just you have to add spaceShip object in to array . And Lastly you can print each of spaceShip objects with for loop effectively. \$\endgroup\$ – Bartu Akman Nov 29 '19 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy