ios - 在 NSUserDefaults iOS 中保存 int 的问题

标签 ios objective-c nsuserdefaults

我正在使用下面的代码从我的 AppDelegate 中将一个 int 和其他一些东西保存到 NSUserDefaults 中。我正在保存的另一个数组充满了符合 NSCoding 的对象,所以我认为这不是问题。如果当前位置不为零,则意味着正在进行一个 session ,因此所有数据都已加载。保存我的数据时,它仅在当前位置不为零时才保存,这表明正在进行 session 。我知道当我在物理设备上退出应用程序时会调用此程序,因为 NSLog 消息出现在调试器中。

- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

if (self.currentPlace != 0) {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray *arr = self.places; // set value
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arr];
    [defaults setObject:data forKey:@"places"];
    [defaults setInteger:self.currentPlace forKey:@"current"];
    NSLog(@"Saving and Quitting b/c we have a place verified!");
    [defaults synchronize];
}
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSInteger myInt = [defaults integerForKey:@"current"];

self.currentPlace = (int)myInt;

if (self.currentPlace != 0) {
    NSData *data = [defaults objectForKey:@"places"];
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    self.places = [arr mutableCopy];
    NSLog(@"Current Place: %i",self.currentPlace);
}

}

我正在使用我的 AppDelegate 来存储可以从我的应用程序中的多个屏幕访问的数据。在第一个 ViewController 中,用户会看到一个菜单。如果 appDelegate 的 currnentPlace 值不是 0,则会显示继续从 AppDelegate 加载的数据的选项。但是,此选项从未出现过。我在我的第一个 View Controller 中使用以下方法检查 currentPlace int 的值:

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

// Do any additional setup after loading the view, typically from a nib.
[self setupViews];
self.spinner.alpha = 0;
NSLog(@"Current %i",delegate.currentPlace);

if (delegate.currentPlace == 0) {
    self.continueButton.enabled = false;
    self.continueButton.alpha = 0.5;
}
else if (delegate.currentPlace != 0) {
    self.continueButton.enabled = true;
    self.continueButton.alpha = 1;
}

如果有人能看出我做错了什么,将不胜感激。

最佳答案

将您的数据恢复代码块移动到一个方法中,如下所示:

   - (void)restoreData {
       NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
       NSInteger myInt = [defaults integerForKey:@"current"];

       self.currentPlace = (int)myInt;

       if (self.currentPlace != 0) {
         NSData *data = [defaults objectForKey:@"places"];
         NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
         self.places = [arr mutableCopy];
         NSLog(@"Current Place: %i",self.currentPlace);
       }
    }

然后在AppDelegate中的application:didFinishLaunchingWithOptions:applicationWillEnterForeground:方法中调用此方法。

关于ios - 在 NSUserDefaults iOS 中保存 int 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30546983/

相关文章:

objective-c - 关于未出现的缺少@required 协议(protocol)方法的编译警告

iphone - 为什么executeFetchRequest是:fetchRequest leaking memory?

ios - 如何在旋转时处理 UIView 圆角(使用 CAShapeLayer)

xcode - 以编程方式清除所有应用程序数据(特别是 NSUserDefaults)

iphone - NSUserDefaults 在启动之间更改值

objective-c - NSUserDefaults 重置首次运行

iphone - 应用大小增加

ios - 什么是 'UIView-Encapsulated-Layout-Width' 约束?

ios - 内容区域未在 xamarin 侧栏组件中滚动

objective-c - 嵌套 NSDictionary 描述转义字符问题