Search code examples
iosobjective-cmethodsdoublensdecimalnumber

Get warning when convert double to NSDecimalNumber


I'm trying to convert a double to an NSDeciamlNumber. Here is my code:

- (NSDecimalNumber *)myMethod {
    double amount = 42;
    ...

    return [NSDecimalNumber numberWithDouble:amount]; // Get warning here
}

But I get the following warning:

Incompatible pointer types returning 'NSNumber *' from a function with result type 'NSDecimalNumber *'

What am I doing wrong, and how can I fix it?


Solution

  • numberWithDouble: method of NSDecimalNumber returns NSNumber. In your method you want to return decimal so its OK if you allocate a new NSDecimalNumber with the decimal value (amount in your example).

    May use the following way to get rid of the error:

    -(NSDecimalNumber *)myMethod 
    { double amount = 42; ...
        return [[NSDecimalNumber alloc] initWithDouble: amount];
    }