简体   繁体   English

NSUserDefault无法检索到双精度值

[英]NSUserDefault not able to retrieve value double

I'm creating a simple double value, saving it as NSUserDefault and trying to recover it...but it doesn't. 我正在创建一个简单的double值,将其另存为NSUserDefault并尝试恢复它...但事实并非如此。

- (IBAction)try:(id)sender {

    double value = 42.00;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setDouble:value forKey:@"kDoubleKey"];
   // NSLog(@"loading %f",myDouble);
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSUserDefaults *fetchDefaults = [NSUserDefaults standardUserDefaults];
    double intValue = [fetchDefaults doubleForKey:@"kDoubleKey"];
    NSLog(@"douvle retrieve %f",intValue);
}

Do not forget to synchronise whenever you save something to the defaults: put this at the end of your try method 每当您将内容保存为默认值时,请不要忘记进行同步:将其放在try方法的末尾

[[NSUserDefaults standardUserDefaults]synchronize];

Here is what apple says about this: Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes. 这是苹果对此的评价: 因为此方法是定期自动调用的,所以仅当您无法等待自动同步(例如,如果您的应用程序即将退出)或要更新用户时,才使用此方法。即使您没有进行任何更改,它也默认为磁盘上的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM