gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:44:56 25 4
gpt4 key购买 nike

我正在使用下面的代码从我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com