使用Xcode 6.1的Swift Enumerations .toRaw和.fromRaw

db0*_*db0 33 enums swift xcode6

在Xcode 6.1中,枚举toRawfromRaw函数不再起作用:

enum TestEnum : String {
    case A = "a"
    case B = "b"
}

if let a = TestEnum.fromRaw("a") {
    prinln(a.toRaw())
}
Run Code Online (Sandbox Code Playgroud)

错误:

'TestEnum' does not have a member named 'toRaw'
'TestEnum.Type' does not have a member named 'fromRaw'
Run Code Online (Sandbox Code Playgroud)

db0*_*db0 54

使用可用的初始化程序从raw创建枚举,rawValue并使用该属性获取原始值rawValue.

if let a = TestEnum(rawValue: "a") {
     println(a.rawValue)
}
Run Code Online (Sandbox Code Playgroud)

阅读更改日志以获取更多信息.


Chr*_*ter 15

看起来好像toRaw()和fromRaw()已经被Xcode 6.1 Beta(Build 6A1030)中的rawValue替换.如果CardSuits是枚举而.Clubs是一个案例,那么你用以下方法检索原始值:let suit1 = CardSuits.Clubs.rawValue如果.Clubs是原始值'1',结果将为'1'从原始值中检索String value将原始值作为枚举的参数传递,如:let suit1 = CardSuits(rawValue:1)(这将是一个可选值)结果将是原始值"1"的枚举值,在此示例中.Clubs