NSUserDefaults 不保存整数

NSUserDefaults not saving integer

我在保存到现有密钥时遇到一些问题

//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];

这个实现有问题吗?它不是保存而是保留以前保存的值。

首先尝试像这样设置您的 NSUserDefaults

NSUserDefaults *userPrefs = [NSUserDefaults standardUserDefaults];

您也可以尝试使用这样的 NSString:

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

[userPrefs setObject:newValueString forKey:aKey];

还要确保您的 aKey 设置正确