Objectmapper: Mapping NSDecimalNumber

Created on 2 Jul 2015  路  4Comments  路  Source: tristanhimmelman/ObjectMapper

My approach fails. n is set to nil, whereas it is set correctly if its type is Double?. Is there a correct way to do this?

class Model: Mappable
{
    var n: NSDecimalNumber?

    required init?(_ map: Map)
    {
        super.init()
        mapping(map)
    }

    func mapping(map: Map)
    {
        n <- map["n"]
    }    
}

Most helpful comment

NSDecimalNumber must be taken in consideration. There is no replacement for this type in Swift. This is the only type that can be used to represent money. Double or float is not an alternative.

All 4 comments

ObjectMapper doesn't support NSDecimalNumber. We are avoiding Objective-C types when possible.

If you need a NSDecimalNumber, you can consider using a CustomTransform to support it.

NSDecimalNumber must be taken in consideration. There is no replacement for this type in Swift. This is the only type that can be used to represent money. Double or float is not an alternative.

@jonasman While I agree with you, there is an included NSDecimalNumberTransform in ObjectMapper, so this is a minor inconvenience.

That sounds good then.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

YunyueLin picture YunyueLin  路  3Comments

AashishSapkota picture AashishSapkota  路  3Comments

mirzadelic picture mirzadelic  路  4Comments

delbyze picture delbyze  路  3Comments

zhengying picture zhengying  路  4Comments