当应用程序终止时,我希望保存几个数组。我在应用程序委托中使用 NSUserDefaults 实现了这一点。任何人都可以看看我的代码,看看有什么问题吗?它没有任何作用。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
workouts = [NSMutableArray arrayWithObjects:nil];
menu = [NSMutableArray arrayWithObjects:@"Home",nil];
workoutNames = [NSMutableArray arrayWithObjects:nil];
routinesMade = [NSMutableArray arrayWithObjects:nil];
test = [NSMutableArray arrayWithObjects:nil];
defaults = [NSUserDefaults standardUserDefaults];
self.workouts = [defaults objectForKey:@"workouts"];
self.menu = [defaults objectForKey:@"menu"];
self.workoutNames = [defaults objectForKey:@"workoutNames"];
self.routinesMade = [defaults objectForKey:@"routinesMade"];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application
{
defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:workouts forKey:@"workouts"];
[defaults setObject:menu forKey:@"menu"];
[defaults setObject:workoutNames forKey:@"workoutNames"];
[defaults setObject:routinesMade forKey:@"routinesMade"];
[defaults synchronize];
}
顺便说一句,我在头文件中声明了默认值。多谢你们!