Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign rawValue of enum to variable with ObjectMapper?

Hello I am using Object Mapper with Alamofire in Swift and I am trying to map enum raw value to real Enum.

Here is my enum and also the code I am trying to use in function mapping. Can you please help me what to pass as argument to EnumTransform or how to modify the code? I know I can read the value as string and the use LevelType(rawValue: stringValue).

Thanks in advance.

enum LevelType : String {     case NEW = "NEW"     case UPDATE = "UPDATE" }  func mapping(map: Map) {     typeEnum <- (map[“type”], EnumTransformable(???) ) } 
like image 297
Radim Halfar Avatar asked Jan 05 '16 13:01

Radim Halfar


1 Answers

You don't have to pass an argument at all. All you have to do is to specify enum type as generic argument and ObjectMapper will take care for all enum initialization procedures.

 typeEnum <- (map["type"],EnumTransform<LevelType>()) 
like image 64
Zell B. Avatar answered Sep 26 '22 07:09

Zell B.