Hi,
I am currently implementing a class Device. That will represent different devices in the real world such as Mobile phone or iBeacon for my application.
I ‘d like to implement these different types as an enum with associated values in it, for the different parameters related to the type (see code example).
I know I could do sub-class, which might be better but I am facing another issue which is.
I am generating a REST API with Swagger, and if I do it with sub-class, I will have to make many many changes. That is why I would prefer to do it with enum. But I am wondering if I am doing it right ? Is it the best way, what would you suggest to me ?
I am pretty new to Swift and didn't get the chance to use enum often.
here is the code
struct Device {
var name : String
var deviceId : String?
let type : Type
init(mobileName: String, platform : String, deviceToken: String, location : CLLocation?){
self.name = mobileName
self.type = .Mobile(platform: platform, deviceToken: deviceToken, location: location)
}
init(pinName: String, location : CLLocation){
self.name = pinName
self.type = .Pin(location: location)
}
init(beaconName: String, uuid: UUID, major: NSNumber, minor: NSNumber){
self.name = beaconName
self.type = .Beacon(uuid: uuid, major: major, minor: minor)
}
}
enum Type {
case Mobile(platform:String, deviceToken: String, location : CLLocation?)
case Pin(location:CLLocation?)
case Beacon(uuid:UUID, major:NSNumber, minor:NSNumber)
}Depending on the type of the Device, I 'd like to initialize it with the proper parameters.
I appreciate your concern and the time you accord to read my post !
Cheers,
Whenwens