【发布时间】:2014-12-16 12:16:39
【问题描述】:
当我将应用程序置于后台模式时,我在 NSUserDefaults 中保存了一个值,然后当应用程序再次激活时,该值不同。
我保存值:
- (void)appDidEnterBackground:(NSNotification *)notification {
//Tiempo inicial de inactividad
NSUserDefaults *dispositivo = [NSUserDefaults standardUserDefaults];
NSTimeInterval timestamp = ([[NSDate date] timeIntervalSince1970] * 1000);
[dispositivo setFloat:timestamp forKey:@"StartBackground"];
NSLog(@"Start background: %f", timestamp);
[[NSUserDefaults standardUserDefaults] synchronize];
}
日志:启动后台:1418731653366.276123
我要恢复价值:
- (void)appDidBecomeActive:(NSNotification *)notification {
NSUserDefaults *dispositivo = [NSUserDefaults standardUserDefaults];
NSTimeInterval startDate = [dispositivo floatForKey:@"StartBackground"];
NSLog(@"Start date: %f", startDate);
}
日志:开始日期:1418731716608.000000
这是我唯一使用这个值的地方。 感谢您的提前。
【问题讨论】:
-
您可以简单地将 NSDate 对象本身存储在 NSUserDefaults 中,而不是将其保存为浮点数。
-
你应该存储 Double 值而不是浮点数!
-
双值结果是一样的
-
谢谢@ZeMoon 这就是解决方案
标签: ios objective-c nsuserdefaults nsusernotification