按索引访问字符串枚举

时间:2023-01-05 23:12:27

I have an enum in C and the index needs to be represented by a String.

我在C中有一个枚举,索引需要用String表示。

How can a Swift enum of String type be used by integer index?

整数索引如何使用Swift枚举的String类型?

I would like to copy the enum to Swift, set the type to string and define all of the raw values to display text, and then use the C enum value to extract the raw value text for the Swift String enum.

我想将枚举复制到Swift,将类型设置为字符串并定义所有原始值以显示文本,然后使用C enum值提取Swift String枚举的原始值文本。

Otherwise I will just create an array of strings.. But the enum would be more usable.

否则我将只创建一个字符串数组..但枚举将更有用。

2 个解决方案

#1


25  

In Swift, enum types do not hold its index info of cases (at least, not provided for programmers).

在Swift中,枚举类型不包含其案例的索引信息(至少,不为程序员提供)。

So:

所以:

How can a Swift enum of String type be used by integer index?

整数索引如何使用Swift枚举的String类型?

The answer is "You cannot".

答案是“你不能”。


You can bind Int (or enum cases) and String values in many ways other than just create an array of strings..

您可以通过多种方式绑定Int(或枚举情况)和String值,而不仅仅是创建一个字符串数组。

For example, if your bound Strings can be the same as case labels, you can write something like this:

例如,如果绑定的字符串可以与案例标签相同,则可以编写如下内容:

enum MyEnum: Int {
    case foo
    case bar
    case baz

    var string: String {
        return String(self)
    }
}

if let value = MyEnum(rawValue: 0) {
    print(value.string) //->foo
}

If your Strings need to be a little more complex to display text, you can use Swift Dictionary to bind enum cases and Strings.

如果您的字符串需要更复杂一些来显示文本,您可以使用Swift Dictionary来绑定枚举案例和字符串。

enum AnotherEnum: Int {
    case foo
    case bar
    case baz

    static let mapper: [AnotherEnum: String] = [
        .foo: "FooString",
        .bar: "BarString",
        .baz: "BazString"
    ]
    var string: String {
        return AnotherEnum.mapper[self]!
    }
}

if let value = AnotherEnum(rawValue: 1) {
    print(value.string) //->BarString
}

A little bit more readable than a simple array of strings.

比简单的字符串数组更具可读性。

#2


8  

Simple workaround which is also useful if you want to enumerate a string enum.

简单的解决方法,如果要枚举字符串枚举,这也很有用。

enum MyEnum: String {
    case foo = "fooString"
    case bar = "barString"
    case baz = "bazString"

    static let allValues = [foo, bar, baz] //must maintain second copy of values
}

//enumeration advantage
for value in MyEnum.allValues {
    print(value)
}

//get value by index
let value = MyEnum.allValues[1] 

print(value) //barString

#1


25  

In Swift, enum types do not hold its index info of cases (at least, not provided for programmers).

在Swift中,枚举类型不包含其案例的索引信息(至少,不为程序员提供)。

So:

所以:

How can a Swift enum of String type be used by integer index?

整数索引如何使用Swift枚举的String类型?

The answer is "You cannot".

答案是“你不能”。


You can bind Int (or enum cases) and String values in many ways other than just create an array of strings..

您可以通过多种方式绑定Int(或枚举情况)和String值,而不仅仅是创建一个字符串数组。

For example, if your bound Strings can be the same as case labels, you can write something like this:

例如,如果绑定的字符串可以与案例标签相同,则可以编写如下内容:

enum MyEnum: Int {
    case foo
    case bar
    case baz

    var string: String {
        return String(self)
    }
}

if let value = MyEnum(rawValue: 0) {
    print(value.string) //->foo
}

If your Strings need to be a little more complex to display text, you can use Swift Dictionary to bind enum cases and Strings.

如果您的字符串需要更复杂一些来显示文本,您可以使用Swift Dictionary来绑定枚举案例和字符串。

enum AnotherEnum: Int {
    case foo
    case bar
    case baz

    static let mapper: [AnotherEnum: String] = [
        .foo: "FooString",
        .bar: "BarString",
        .baz: "BazString"
    ]
    var string: String {
        return AnotherEnum.mapper[self]!
    }
}

if let value = AnotherEnum(rawValue: 1) {
    print(value.string) //->BarString
}

A little bit more readable than a simple array of strings.

比简单的字符串数组更具可读性。

#2


8  

Simple workaround which is also useful if you want to enumerate a string enum.

简单的解决方法,如果要枚举字符串枚举,这也很有用。

enum MyEnum: String {
    case foo = "fooString"
    case bar = "barString"
    case baz = "bazString"

    static let allValues = [foo, bar, baz] //must maintain second copy of values
}

//enumeration advantage
for value in MyEnum.allValues {
    print(value)
}

//get value by index
let value = MyEnum.allValues[1] 

print(value) //barString