NSNumberFormatter


#1

Suppose I wanted to implement NSNumberFormatter programmatically. I tried to utilize this code with a NumberFormatter, but I got an exception:

[code]@implementation RMDocument
@synthesize tableView;
@synthesize nf;

  • (id)init
    {
    self = [super init];
    if (self) {
    employees = [[NSMutableArray alloc]init];
    }
    [nf setNumberStyle:NSNumberFormatterPercentStyle];
    return self;

}[/code]

[code]

  • (void) tableView:(NSTableView *)tableView
    setObjectValue:(id)object
    forTableColumn:(NSTableColumn *)tableColumn
    row:(NSInteger)row {
    NSString *columnId = [tableColumn identifier];
    Person *thePerson = [employees objectAtIndex:row];

    if ([columnId compare:@“expectedRaise”]) {
    NSLog(@“tv setObjectValue: columnId is equal to %@”,[columnId value]);
    NSNumber *theNumber = object;

      [thePerson setValue:theNumber forKey];
    

    } else {
    NSLog(@“tv objectValueForTC: columnId NOT equal to %@”,[columnId value]);
    [thePerson setValue:object forKey];
    }
    }[/code]

[code]- (id) tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row {

NSString *columnId = [tableColumn identifier];
Person *thePerson = [employees objectAtIndex:row];

if ([columnId compare:@"expectedRaise"]) {
    NSLog(@"tv objectValueForTC: columnId is equal to %@",[columnId value]);
    NSNumber *theNumber = [NSNumber numberWithFloat:thePerson.expectedRaise];
    NSString *cellField = [nf stringFromNumber:theNumber];
    return cellField;
} else {
    
     NSLog(@"tv objectValueForTC: columnId NOT equal to %@",[columnId value]);
    return [thePerson valueForKey];
}

}[/code]

I didn’t include the other code that I didn’t change. Program was working fine without any of the NumberFormatter stuff.