Answers for "enum swift"

0

can you pass an enum as a parameter to a function swift

func justAnExample<T : RawRepresentable>(_ enumType: T.Type) where T.RawValue == Int {

  // Note that an explicit use of init is required when creating an instance from a
  // metatype. We're also using a guard, as `init?(rawValue:)` is failable.
  guard let val = enumType.init(rawValue: 0) else { return }
  print("description: \(val)")
}

justAnExample(Foo.self) // prints: "description: Hello Foo"
justAnExample(Bar.self) // prints: "description: Hello Bar"
Posted by: Guest on December-08-2020
1

enum raw value swift

enum numbers: Int {
 case one = 1, two, three, four
}

let num = numbers.one
print(num.rawValue) // Output: 1
Posted by: Guest on December-31-2020

Code answers related to "Swift"

Browse Popular Code Answers by Language