enum swift code example
Example 1: can you pass an enum as a parameter to a function swift
func justAnExample<T : RawRepresentable>(_ enumType: T.Type) where T.RawValue == Int {
guard let val = enumType.init(rawValue: 0) else { return }
print("description: \(val)")
}
justAnExample(Foo.self)
justAnExample(Bar.self)
Example 2: swift enum
enum Planet {
case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}
Example 3: enum raw value swift
enum numbers: Int {
case one = 1, two, three, four
}
let num = numbers.one
print(num.rawValue)