aryaxt / OCMapper

Objective-C library to easily map NSDictionary to model objects, works perfectly with Alamofire. ObjectMapper works similar to GSON
MIT License
347 stars 45 forks source link

Automatically parse 0 or 1 to Bool #60

Open balazsgerlei opened 7 years ago

balazsgerlei commented 7 years ago

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!

balazsgerlei commented 7 years ago

Any thoughts on this @aryaxt ?