How to increment a NSNumber

Objective C

Objective C Problem Overview


How do I increment a NSNumber?

i.e. myNSNumber++

Objective C Solutions


Solution 1 - Objective C

NSNumber objects are immutable; the best you can do is to grab the primitive value, increment it then wrap the result in its own NSNumber object:

NSNumber *bNumber = [NSNumber numberWithInt:[aNumber intValue] + 1];

Solution 2 - Objective C

Update: FYI, I personally like BoltClock's and DarkDusts's one-line answers better. They're more concise, and don't require additional variables.


In order to increment an NSNumber, you're going to have to get its value, increment that, and store it in a new NSNumber.

For instance, for an NSNumber holding an integer:

NSNumber *number = [NSNumber numberWithInt:...];
int value = [number intValue];
number = [NSNumber numberWithInt:value + 1];

Or for an NSNumber holding a floating-point number:

NSNumber *number = [NSNumber numberWithDouble:...];
double value = [number doubleValue];
number = [NSNumber numberWithDouble:value + 1.0];

Solution 3 - Objective C

For anyone who is using the latest version of Xcode (writing this as of 4.4.1, SDK 5.1), with the use of object literals, you can clean the code up even a little bit more...

NSNumber *x = @(1);
x = @([x intValue] + 1);
// x = 2

Still kind of a pain to deal with the boxing and unboxing everything to do simple operations, but it's getting better, or at least shorter.

Solution 4 - Objective C

NSNumbers are immutable, you have to create a new instance.

// Work with 64 bit to support very large values
myNSNumber = [NSNumber numberWithLongLong:[myNSNumber longLongValue] + 1];

// EDIT: With modern syntax:
myNSNumber = @([myNSNumber longLongValue] + 1);

Solution 5 - Objective C

Put them all together, and you have:

myNSNumber = @(myNSNumber.intValue + 1);

Solution 6 - Objective C

I like the dot expression because it is more concise and that is why IOS supports it in the first place:

myNSNumber = [NSNumber numberWithInt:myNSNumber.intValue + 1];

Solution 7 - Objective C

If you're using it to store an integral value:

myNSNumber = @(myNSNumber.longLongValue + 1);

For floating point stuff the short answer is the following, but it's a bad idea to do this, you'll loose precision and if you're doing any kind of comparison later like [myNSNumber isEqual:@(4.5)] you might be surprised:

myNSNumber = @(myNSNumber.floatValue + 1);

If you need to do math on floating point numbers represented as objects in Objective-C (i.e. if you need to put them in arrays, dictionaries, etc.) you should use NSDecimalNumber.

Solution 8 - Objective C

Use a category to ease future use. Here is a basic idea.

 - (void)incrementIntBy:(int)ammount {
      self = [NSNumber numberWithInt:(self.intValue + ammount)];
 }

Solution 9 - Objective C

Lots of good info in the answers to this question. I used the selected answer in my code and it worked. Then I started reading the rest of the posts and simplified the code a lot. It’s a bit harder to read if you aren’t familiar with the changes in notation that were introduced in Xcode 5, but it’s a lot cleaner. I probably could make it just one line, but then it’s a little too hard to figure out what I’m doing.

I’m storing an NSNumber in a dictionary and I want to increment it. I get it, convert it to an int, increment it while converting it to an NSNumber, then put it back into the dictionary.

Old Code

 NSNumber *correct = [scoringDictionary objectForKey:@"correct"];
 int correctValue = [correct intValue];
 correct = [NSNumber numberWithInt:correctValue + 1];
 [scoringDictionary removeObjectForKey:@"correct"];
 scoringDictionary[@"correct"] = correct;

New code

NSNumber *correct = [scoringDictionary objectForKey:@"correct"];
scoringDictionary[@"correct"] = @(correct.intValue + 1);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTheLearnerView Question on Stackoverflow
Solution 1 - Objective CBoltClockView Answer on Stackoverflow
Solution 2 - Objective CItai FerberView Answer on Stackoverflow
Solution 3 - Objective CshortstuffsushiView Answer on Stackoverflow
Solution 4 - Objective CDarkDustView Answer on Stackoverflow
Solution 5 - Objective CJLundellView Answer on Stackoverflow
Solution 6 - Objective CjackView Answer on Stackoverflow
Solution 7 - Objective ClmirosevicView Answer on Stackoverflow
Solution 8 - Objective CnizxView Answer on Stackoverflow
Solution 9 - Objective CJScarryView Answer on Stackoverflow