Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to decode to custom types? #22

Closed
staticdreams opened this issue Sep 30, 2021 · 2 comments
Closed

Is it possible to decode to custom types? #22

staticdreams opened this issue Sep 30, 2021 · 2 comments

Comments

@staticdreams
Copy link

Hi,

I'm looking for a way to map/decode json values to custom Enums.

For instance my API return status value which may be something like -1, 0 or 1
I also have an enum:

enum Status: Int {
    case deleted = -1
    case inactive = 0
    case active = 1
}

Can I map these together? I tried implementing a custom mapper from your example buy it doest seem to accept custom types, such as Status . I want to be able to do something like this:

return Status(rawValue: -1)

and get my custom type

@RunningSquirrel
Copy link

RunningSquirrel commented Nov 19, 2021

How about making your Status conform to Codable?

enum Status: Int, Codable {
    case deleted = -1
    case inactive = 0
    case active = 1
}

@GottaGetSwifty
Copy link
Owner

@staticdreams As @earthworkerman mentioned, this should work if you adopt Codable. Please re-open if you have issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants