Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以在Swift中将枚举类型名称作为参数传递?_Swift_Types_Enums - Fatal编程技术网

是否可以在Swift中将枚举类型名称作为参数传递?

是否可以在Swift中将枚举类型名称作为参数传递?,swift,types,enums,Swift,Types,Enums,我试图弄清楚是否可以像在Swift中传递Class对象那样传递enum的类型 我的实际用例比这要复杂一些,但是为了讨论,让我们假设我有两个Intenum: enum Foo: Int, CustomStringConvertible { case firstFoo = 0 case anotherFoo = 1 var description: String { switch self { case .firstFoo:

我试图弄清楚是否可以像在Swift中传递
Class
对象那样传递enum的类型

我的实际用例比这要复杂一些,但是为了讨论,让我们假设我有两个
Int
enum:

enum Foo: Int, CustomStringConvertible {
    case firstFoo = 0
    case anotherFoo = 1
    var description: String {
        switch self {
        case .firstFoo:
            return "Hello Foo"
        case .anotherFoo:
            return "Goodbye Foo"
        }
    }
}

enum Bar: Int, CustomStringConvertible {
    case firstBar = 0
    case anotherBar = 1
    var description: String {
        switch self {
        case . firstBar:
            return "Hello Bar"
        case . anotherBar:
            return "Goodbye Bar"
        }
    }
}
我希望能够编写如下函数:

func justAnExample(whichEnum: enum) {
    let val = whichEnum(rawValue: 0)
    print("description: \(String(val))")
}
justAnExample(Foo)
// prints: "description: Hello Foo"
justAnExample(Bar)
// prints: "description: Hello Bar"
然后像这样使用它:

func justAnExample(whichEnum: enum) {
    let val = whichEnum(rawValue: 0)
    print("description: \(String(val))")
}
justAnExample(Foo)
// prints: "description: Hello Foo"
justAnExample(Bar)
// prints: "description: Hello Bar"

这可能吗?如果是这样,函数声明中whichEnum的签名是什么?

您可以利用这样一个事实,即具有原始值的枚举自动符合协议。您可以定义一个泛型函数,该函数接受给定类型的元类型参数
T
(拼写为
T.type
),其中
T
是,在您的例子中,它也是
Int

这将允许您传入
Foo
Bar
的元类型,分别拼写为
Foo.self
Bar.self

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"
func justAnExample(enumType:T.Type),其中T.RawValue==Int{
//请注意,当从数据库创建实例时,需要显式使用init
//我们还使用了一个保护,因为'init?(rawValue:)'是可失败的。
guard let val=enumType.init(rawValue:0)else{return}
打印(“说明:\(val)”)
}
justAnExample(Foo.self)//打印:“description:Hello Foo”
justAnExample(Bar.self)//打印:“description:Hello Bar”

一种解决方案是为RawRepresentable编写扩展,例如

extension RawRepresentable where RawValue == Int{

    static func testPrint() {
        print(Self(rawValue: 0))
    }
}
enum Thing: Int {
    case Test = 0
}
// prints "Optional(Thing.Test)"
Thing.testPrint()

那你就不用担心通过任何考试了。当然,它不仅仅适用于枚举,这没有帮助,因为我没有
东西。我试图将
ThingA
ThingB
作为参数传递,如果它是ThingA:Int和ThingB:Int,应该可以。您也可以为任何其他RawValue类型编写扩展,但这并不能解决问题。我想调用
someOtherMethod(Thing)
,而不是
Thing.testPrint()
如何在swift4中编写它,在swift4中,该方法还返回一个值?在“where T.RawValue==Int->String”中,它认为RawValue是(Int)->String EDIT:方法返回类型位于where子句之前!