Use a sweet-ass category,
@interface NSNumber (NSNumber_Incrementing)
-(NSNumber *)increment;
@end
@implementation NSNumber (NSNumber_Incrementing)
-(NSNumber *)increment {
return [NSNumber numberWithInt:[self intValue] + 1];
}
[person.list setObject:[mynumb increment] forKey:name];
that means you need to hold mynumb
Add a protocol for NSDecimal et al.
@protocol Incrementing
- (NSNumber *)increment;
@end
so the above might be
@interface NSNumber (NSNumber_Incrementing) <Incrementing>
...
@interface NSDecimableNumber (NSNumber_Incrementing) <Incrementing>
But even f*ng sweeter is a category on Dictionary:
@interface NSDictionary (NSDictionary_SweetIncrementing)
-(void) incrementNumberForKey:name;
@end
@implementation NSDictionary (NSDictionary_SweetIncrementing)
-(void) incrementNumberForKey:name throws NSNumberException {
NSNumber numb = (NSNumber *)[self valueForKey: name];
return [self setValue: [numb increment] forKey: name];
}
@end
valueForKey and setValue belong to NSDictionary. You are using a NSMutableDictionary that offers objectForKey and setObject.... so do that instead if so desired.
Yeah. And add some exceptions for key not associated with number
A similar question