Skip to content

Automatically parse 0 or 1 to Bool #60

@balazsgerlei

Description

@balazsgerlei

It is quite common for a server to return 0 or 1 for boolean values. It would be great if OCMapper could automatically parse this as booleans. I use it in Swift but I think this is possible to implement both in Swift and Objective-C.

For now, I created a MappingTransformer for this which I have to set for every parameter where I would like to use it:

Swift 2.x:

let intToBoolTransformer: MappingTransformer = {
    currentNode, parentNode in
    return currentNode as? NSNumber == 1 ? true : false
}

Swift 3.x (Somehow casting to Int always results in nil):

let intToBoolTransformer: MappingTransformer = {
    currentNode, parentNode in
    return currentNode as? NSNumber == 1 ? true : false
}

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions