本文介绍了在Swift中可以传递枚举类型名称作为参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚是否可以通过与Swift中的 Class 对象相同的方式传递枚举的类型。



我的实际使用情况比这更复杂一点,但是讨论的时候我们说两个 Int 枚举:

 枚举Foo:Int,CustomStringConvertible {
case firstFoo = 0
case anotherFoo = 1
var description:String {
switch self {
case .firstFoo:
returnHello Foo
case .anotherFoo:
returnGoodbye Foo
}
}
}

枚举Bar:Int,CustomStringConvertible {
case firstBar = 0
case anotherBar = 1
var description:String {
switch self {
case。 firstBar:
返回Hello Bar
case。另一个栏:
返回再见吧
}
}
}

我想要写一个这样的函数:

  func justAnExample(whichEnum:enum){ 
let val = whichEnum(rawValue:0)
print(description:\(String(val)))
}
pre>

然后使用它:

  justAnExample(Foo )
// prints:description:Hello Foo
justAnExample(Bar)
// prints:description:Hello Bar

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

解决方案

p>您可以使用来做到这一点,定义函数参数作为给定的 T T.Type )的元类型,其中 T 是,而其 RawValue Int 。这将允许您传入 Foo Bar 的元数据类型。

  func justAnExample< T:RawRepresentable>(_ whichEnum:T.Type)其中T.RawValue == Int {

//请注意在从元类型创建实例时,必须使用init来显式使用init
//。
//我们还使用了一个防守,因为init?(rawValue :)是可用的。
guard let val = whichEnum.init(rawValue:0)else {return}
print(description:\(val))
}

justAnExample (Foo.self)// prints:description:Hello Foo

justAnExample(Bar.self)// prints:description:Hello Bar
请注意,在Swift 3之前,您可以省略 .self (获取元类型)从论证中,如果您这样做,Swift 3会产生警告。


I am trying to figure out if it's possible to pass around enum's type in the same way that you can pass Class objects in Swift.

My actual use case is a bit more complicated than this, but for discussion, let's say I have two Int enums:

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"
        }
    }
}

I would like to be able to write a function like this:

func justAnExample(whichEnum: enum) {
    let val = whichEnum(rawValue: 0)
    print("description: \(String(val))")
}

And then use it like this:

justAnExample(Foo)
// prints: "description: Hello Foo"
justAnExample(Bar)
// prints: "description: Hello Bar"

Is this possible? If so, what is the signature of whichEnum in the function declaration?

解决方案

You can use generics in order to do this, defining the function argument to be the metatype of a given T (T.Type), where that T is RawRepresentable, and its RawValue is Int. This will allow you to pass in the metatypes of both Foo and Bar.

func justAnExample<T : RawRepresentable>(_ whichEnum: 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 = whichEnum.init(rawValue: 0) else { return }
    print("description: \(val)")
}

justAnExample(Foo.self) // prints: "description: Hello Foo"

justAnExample(Bar.self) // prints: "description: Hello Bar"

Note that prior to Swift 3, you could omit the .self (which gets the metatype) from the arguments, however Swift 3 will generate a warning if you do so.

这篇关于在Swift中可以传递枚举类型名称作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:04