Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUserDefaults not saving integer

I'm having some problems saving to an existing key

//This was previously set and contains this value 1436544831921
NSInteger timestamp = [[NSUserDefualts standardUserDefualts] integerForKey:aKey];

NSInteger newValue = [self doSomethingWithOldValue:timestamp] //returns 1436631948002

[[NSUserDefualts standardUserDefualts] setInteger:newValue forKey:aKey];
[[NSUserDefualts standardUserDefualts] synchronize];

Is something wrong with this implementation? It is not saving but persisting the previously saved value.

like image 200
quant24 Avatar asked Sep 27 '22 10:09

quant24


1 Answers

Try setting your NSUserDefaults first like this:

NSUserDefaults *userPrefs = [NSUserDefaults standardUserDefaults];

You can also try to use a NSString like this:

NSInteger newValue = [self doSomethingWithOldValue:timestamp];
NSString* newValueString = [NSString stringWithFormat:@"%i", newValue];

[userPrefs setObject:newValueString forKey:aKey];

Also make sure that your aKey is set correctly

like image 112
TomWayne Avatar answered Oct 06 '22 01:10

TomWayne