theddnc / iModel

Validation, JSON parsing and async remote communication in one bundle.
MIT License
6 stars 0 forks source link

Parsing enum in Model #1

Open Moazzam123 opened 8 years ago

Moazzam123 commented 8 years ago

Hi I am using iModel but I am not able to parse my enum values which is a part of my model. Please help me.

theddnc commented 8 years ago

This is an enhancement - you can use this workaround for now:

public enum MyEnum: Int {
    case pretty = 0
    case ugly = 1
}

public class EnumModel: JsonModel {
    private dynamic var enumRawValue: Int = 0
    public var enumField: MyEnum {
        get {
            return MyEnum(rawValue: self.enumRawValue)!
        }
        set {
            self.enumRawValue = newValue.rawValue
        }
    }

    override public class func jsonPropertyNames() -> [String: String] {
        return ["enumField": "enumRawValue"]
    }

    //...
}

It'll be difficult to parse Swift's native enums, since these are not conforming to AnyObject. I'm using NSObject's KVC (setValue(forKey:)) which accepts AnyObject params.