Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get enum from raw value in Swift?

Tags:

enums

ios

swift

I'm trying to get enum type from raw value:

enum TestEnum: String {     case Name     case Gender     case Birth      var rawValue: String {         switch self {         case .Name: return "Name"         case .Gender: return "Gender"         case .Birth: return "Birth Day"         }     } }  let name = TestEnum(rawValue: "Name")       //Name let gender = TestEnum(rawValue: "Gender")   //Gender 

But it seems that rawValue doesn't work for string with spaces:

let birth = TestEnum(rawValue: "Birth Day") //nil 

Any suggestions how to get it?

like image 672
Leo Avatar asked Mar 23 '16 17:03

Leo


People also ask

What is raw value in enum Swift?

Each raw value for our enum case must be a unique string, character, or value of any integer or floating-point type. This means the value for the two case statements cannot be the same.

Why Swift enums with associated values Cannot have a raw value?

We had to do this because Swift doesn't allow us to have both: raw values and associated values within the same enum. A Swift enum can either have raw values or associated values. Why is that? It's because of the definition of a raw value: A raw value is something that uniquely identifies a value of a particular type.

What is enumerated () in Swift?

In Swift, an enum (short for enumeration) is a user-defined data type that has a fixed set of related values. We use the enum keyword to create an enum. For example, enum Season { case spring, summer, autumn, winter } Here, Season - name of the enum.

What is rawValue?

A "raw value" is an unique identifier of a type. It means that you are able to construct your type by ID. For example: XCTAssertEqual(Color.white, Color(rawValue: "#ffffff")) To get raw value use Color.white.rawValue.


2 Answers

Too complicated, just assign the raw values directly to the cases

enum TestEnum: String {   case Name = "Name"   case Gender = "Gender"   case Birth = "Birth Day" }  let name = TestEnum(rawValue: "Name")!       //Name let gender = TestEnum(rawValue: "Gender")!   //Gender let birth = TestEnum(rawValue: "Birth Day")! //Birth 

If the case name matches the raw value you can even omit it

enum TestEnum: String {   case Name, Gender, Birth = "Birth Day" } 

In Swift 3+ all enum cases are lowercased

like image 98
vadian Avatar answered Sep 18 '22 09:09

vadian


Full working example:

enum TestEnum: String {     case name = "A Name"     case otherName     case test = "Test" }  let first: TestEnum? = TestEnum(rawValue: "A Name") let second: TestEnum? = TestEnum(rawValue: "OtherName") let third: TestEnum? = TestEnum(rawValue: "Test")  print("\(first), \(second), \(third)") 

All of those will work, but when initializing using a raw value it will be an optional. If this is a problem you could create an initializer or constructor for the enum to try and handle this, adding a none case and returning it if the enum couldn't be created. Something like this:

static func create(rawValue:String) -> TestEnum {         if let testVal = TestEnum(rawValue: rawValue) {             return testVal         }         else{             return .none         }     } 
like image 32
GetSwifty Avatar answered Sep 20 '22 09:09

GetSwifty