📜  您可以将枚举作为参数传递给函数吗? swift 代码示例

📅  最后修改于: 2022-03-11 15:01:00.444000             🧑  作者: Mango

代码示例1
func justAnExample(_ 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"