我在保存到现有 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];

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

最佳答案

尝试像这样首先设置NSUserDefaults:

NSUserDefaults *userPrefs = [NSUserDefaults standardUserDefaults];

您也可以尝试使用NSString,如下所示:
NSInteger newValue = [self doSomethingWithOldValue:timestamp];
NSString* newValueString = [NSString stringWithFormat:@"%i", newValue];

[userPrefs setObject:newValueString forKey:aKey];

还要确保正确设置了aKey

关于ios - NSUserDefaults不保存整数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31254510/

10-12 06:15