views:

49

answers:

2

Hello,

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: 

numberWithFloat method returns a NSNumber instance. Just typecasting it to NSDecimalNUmber might resolve the warning.

NSDecimalNumber *tempNumber = (NSDecimalNumber *)[NSDecimalNumber numberWithFloat:[self.slider value]];
lukya
+2  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:

  1. (dirty) you might try to cast the result to (NSDecimalNumber *) and see if it works
  2. (clean) use one of NSDecimalNumbers initializers, like + decimalNumberWithString:
  3. (best) Shift your method to use NSNumber and a NSNumberFormatter only, this supports different rounding behaviors as well
Max Seelemann
method 1 works, (`[[NSDecimalNumber numberWithFloat:2] class]` is NSDecimalNumber)
cobbal
Okay, good to hear, but I'd still stick to a different method. The static cast in the NSNumber headers is definitely a problem here...
Max Seelemann
@cobbal - Actually, that can introduce floating-point issues, so I'd recommend number 2 on the list if you wish to avoid this. See this question for an example of what can go wrong: http://stackoverflow.com/questions/2479100/cocoa-ive-discovered-what-i-think-is-a-bug-with-nsdecimalnumber
Brad Larson