Given the Swift enumeration as such:
enum PerformerPosition: Int {
case String_Violin1
case String_Violin2
case String_Viola
case String_Cello
case String_CB
case Wind_Oboe
case Wind_Clarinet
case Wind_Flute
...
}
(For the needs of the project, I may not have a nested enumeration.) I would like to randomly select an enumeration value with only a prefix String_
.
The only way I know so far is to make a random enumeration value from all available cases, as such:
private static let _count: PerformerPosition.RawValue = {
var maxValue: Int = 0
while let _ = PerformerPosition(rawValue: maxValue) {
maxValue += 1
}
return maxValue
}()
static func randomPerformer() -> PerformerPosition {
let rand = arc4random_uniform(UInt32(count))
return PlayerPosition(rawValue: Int(rand))!
}
How can I do this, so I can choose a random value based on the prefix String_
without , to resort to hard coding the upper value (for example, a new String_
prefix position can be added)? Thanks
source
share