Q:

Incompatible Objective-C types initializing 'struct NSNumber *', expected 'struct NSDecimalNumber *'

I have the below method in my project, and I get a compiler warning: Incompatible Objective-C types initializing 'struct NSNumber *', expected 'struct NSDecimalNumber * on the first line:
- (IBAction)sliderValueChanged:(id)sender {
    // HERE IS WHERE THE WARNING OCCURS:
    NSDecimalNumber *tempNumber = [NSDecimalNumber numberWithFloat:[self.slider value]];

    NSDecimalNumberHandler *roundingStyle = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers
                                                                                                   scale:3 
                                                                                        raiseOnExactness:NO
                                                                                         raiseOnOverflow:NO 
                                                                                        raiseOnUnderflow:NO 
                                                                                     raiseOnDivideByZero:NO];
    NSDecimalNumber *roundedNumber = [tempNumber decimalNumberByRoundingAccordingToBehavior:roundingStyle];
    self.slider.value = [roundedNumber floatValue];

    // Set up the percentage formatter
    NSNumberFormatter *percentFormatter = [[NSNumberFormatter alloc] init];
    [percentFormatter setNumberStyle:NSNumberFormatterPercentStyle];
    [percentFormatter setMinimumFractionDigits:1];
    [percentFormatter setMaximumFractionDigits:1];

    // Find the label to update
    NSUInteger onlyRow[] = {0, 0};
    NSIndexPath *onlyRowPath = [NSIndexPath indexPathWithIndexes:onlyRow length:2];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:onlyRowPath];
    UILabel *theLabel = (UILabel *)[cell.contentView viewWithTag:kLabelFieldTag];

    NSNumber *theNumber = [NSNumber numberWithFloat:self.slider.value];
    theLabel.text = [percentFormatter stringFromNumber:theNumber];

    [percentFormatter release];
}

Any help would be appreciated, thanks!

A:

The method numberWithFloat is said (in the header) to always returns an NSNumber object. Sending the method to a subclass still returns an NSNumber object. I don't know NSDecimalNumber in detail, but I see several approaches to the problem here:

(dirty) you might try to cast the result to (NSDecimalNumber *) and see if it works
(clean) use one of NSDecimalNumbers initializers, like + decimalNumberWithString:
(best) Shift your method to use NSNumber and a NSNumberFormatter only, this supports different rounding behaviors as well

