Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Enumerations .toRaw and .fromRaw with Xcode 6.1

In Xcode 6.1, the enumerations toRaw and fromRaw functions don't work anymore:

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

if let a = TestEnum.fromRaw("a") {
    prinln(a.toRaw())
}

Errors:

'TestEnum' does not have a member named 'toRaw'
'TestEnum.Type' does not have a member named 'fromRaw'
like image 794
db0 Avatar asked Sep 18 '14 17:09

db0


2 Answers

Create an enum from a raw using the failable initializer with rawValue and get the raw value using the attribute rawValue.

if let a = TestEnum(rawValue: "a") {
     println(a.rawValue)
}

Read the changelog for more information.

like image 198
db0 Avatar answered Nov 18 '22 10:11

db0


It looks as if toRaw() and fromRaw() have been replaced with rawValue in Xcode 6.1 Beta (Build 6A1030). If CardSuits was an enum and .Clubs a case, then you retrieve the raw value with: let suit1 = CardSuits.Clubs.rawValue The result will be '1' if .Clubs was raw value '1' To retrieve the String from the raw value pass the raw value as a parameter of the enum like: let suit1 = CardSuits(rawValue:1) (this will be an optional value) The result will be the enum value of raw value '1', in this example .Clubs

like image 43
Chris Ritter Avatar answered Nov 18 '22 12:11

Chris Ritter