tags:

views:

30

answers:

1

Hi guys . i have an NSNumber that is taken from a string like this

NSNumberFormatter *f = [[NSNumberFormatter alloc]init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];

BRNumber = [f numberFromString:BRString];

of course this returns a value but now i wish to do something like

id y = BRNumber + 50; 

It seems that this is not possible. So how should i go about doing this.?

+2  A: 

You need to get the raw integer before doing the computation, and wrap it back to an NSNumber afterwards.

NSNumber* y = [NSNumber numberWithInt:[BRNumber intValue] + 50];

If you're doing this a lot, you could create a category:

@implementation NSNumber (Arithmetics)
-(NSNumber*)numberByAddingInt:(int)val {
   return [NSNumber numberWithInt:[self intValue] + val];
}
@end

then you could use

NSNumber* y = [BRNumber numberByAddingInt:50];

There's no shorter ways, since ObjC doesn't have operator overloading.

KennyTM
ok great! it worked. Thanks alot. can only accept ur answer in 9 mins tho.
Kenneth