quant24
quant24

Reputation: 392

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.

Upvotes: 5

Views: 531

Answers (1)

TomWayne
TomWayne

Reputation: 230

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

Upvotes: 4

Related Questions