How to add a number to an integer
-
13-12-2019 - |
Question
I am trying to make a NSInteger being abel to contain a number while the app is runing and that I can add numbers to that NSInteger hers my code
// in the .h
NSInteger *count;
// in the .m
count = 0;
Then I save count in NSUserDefaults like so
[[NSUserDefaults standardUserDefaults] setInteger:count forKey:@"key"];
[[NSUserDefaults standardUserDefaults] synchronize];
In the next view after calling NSUserDefaults I do this
count + 4;
The problem is that in the end I always get zero.
Solution
NSInteger
is a primitive type, not an object type. So you don't want to use a pointer to NSInteger
, but directly NSInteger
:
// in the .h
NSInteger count;
// in the .m
count = 0;
And to increase it:
count += 4;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow