-2

I'm experiencing an issue when saving & retrieving an int from NSUserDefaults. I am saving to NSUserDefaults using the following code:

int globalRank = 1;
NSUserDefaults *submissionDefaults = [NSUserDefaults standardUserDefaults];
[submissionDefaults setInteger: globalRank forKey:@"globalRankIntForLT"];
NSLog(@"updating %@ as the globalRank in NSUserDefaults",globalRank);
[submissionDefaults synchronize];

This appears to work correctly. In my output I can see:

"updating 1 as the globalRank in NSUserDefaults"

When I retreive the number using the code below:

NSUserDefaults *submissionDefaults = [NSUserDefaults standardUserDefaults];
NSInteger *currentGlobalRank = [submissionDefaults integerForKey:@"globalRankIntForLT"];
int currentGlobalRankInt = currentGlobalRank;
NSLog(@"Retrieved skip int is: %d as nsinteger is: %d",currentGlobalRankInt, currentGlobalRank);

I get output:
"Retrieved skip int is: 4978484032 as nsinteger is: 4978484032"

I later pass this int to another method that returns an error because 4978484032 is bigger than it was expecting.

NSUserDefaults contains an NSInteger but it is coming incorrectly even at that point. What am I doing wrong? Thanks, James

JamesLCQ
  • 341
  • 6
  • 12

4 Answers4

1

NSInteger is a primitive type, not an object. It should be NSInteger currentGlobalRank instead of NSInteger *currentGlobalRank. You can use NSInteger instead of int in your code. There's no need to convert an NSInteger to int.

On iOS, NSInteger is defined as int, on OS X it's long.

Fabian Kreiser
  • 8,307
  • 1
  • 34
  • 60
  • I didn't know NSInteger was primitive. I will try this tonight & update results. Thanks, James – JamesLCQ Jan 09 '13 at 11:59
  • @JamesLCQ "I didn't know" - **you should always read the documentation of types and classes you want to use. Don't guess.** –  Jan 09 '13 at 12:01
  • I did read the documentation but it must have slipped my mind. Thanks for your help. – JamesLCQ Jan 10 '13 at 10:43
1

You're setting an integer and trying to retrieve a POINTER to integer. Change:

NSInteger *currentGlobalRank = [submissionDefaults integerForKey:@"globalRankIntForLT"];

to:

NSInteger currentGlobalRank = [submissionDefaults integerForKey:@"globalRankIntForLT"];

Despite NSInteger starts from NS that's not a subclass of NSObject, it's just primitive

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
0

Change this code...

NSInteger *currentGlobalRank = [submissionDefaults integerForKey:@"globalRankIntForLT"];
int currentGlobalRankInt = currentGlobalRank;
NSLog(@"Retrieved skip int is: %d as nsinteger is: %d",currentGlobalRankInt, currentGlobalRank);

to...

NSInteger *currentGlobalRank = [submissionDefaults integerForKey:@"globalRankIntForLT"];
int currentGlobalRankInt = [currentGlobalRank intValue];
NSLog(@"Retrieved skip int is: %d as nsinteger is: %@",currentGlobalRankInt, currentGlobalRank);
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
0

Use NSInteger or a wrapper class like NSNumber instead of int.

And you are putting a * by mistake... in NSInteger *currentGlobalRank

NSInteger globalRank = 1;
NSUserDefaults *submissionDefaults = [NSUserDefaults standardUserDefaults];
[submissionDefaults setInteger: globalRank forKey:@"globalRankIntForLT"];
NSLog(@"updating %@ as the globalRank in NSUserDefaults",globalRank);
[submissionDefaults synchronize];



NSUserDefaults *submissionDefaults = [NSUserDefaults standardUserDefaults];
NSInteger currentGlobalRank = [submissionDefaults integerForKey:@"globalRankIntForLT"];
NSLog(@"Retrieved skip int is: %d as nsinteger is: %d",currentGlobalRank, currentGlobalRank);
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140