IOS:使用 NSUserDefault 存储数组

发布于 2024-12-06 15:22:14 字数 470 浏览 3 评论 0原文

我想用 NSUserDefault 存储一个数组,然后,我放入 applicationDidEnterBackground

[[NSUserDefaults standardUserDefaults] setObject:myArray forKey:@"myArray"];

并在 application didFinishLaunchingWithOption 中,

myArray= [[NSMutableArray alloc] 
          initWithArray:[[NSUserDefaults standardUserDefaults] 
           objectForKey:@"myArray"]];

这对于多任务设备来说是可以的,但对于非 -多任务设备,我该如何解决?

I want to store an array with NSUserDefault, then, I put in applicationDidEnterBackground

[[NSUserDefaults standardUserDefaults] setObject:myArray forKey:@"myArray"];

and in application didFinishLaunchingWithOption

myArray= [[NSMutableArray alloc] 
          initWithArray:[[NSUserDefaults standardUserDefaults] 
           objectForKey:@"myArray"]];

it's ok for multitasking device, but for not-multitasking device, how can I solve?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

默嘫て 2024-12-13 15:22:14

如果尚未通过调用 -applicationDidEnterBackground: 保存对象,则将对象存储在 -applicationWillTerminate:NSUserDefaults 中(即检查是否支持多任务,如果支持,则不要保存,因为它已经保存了。)

- (void) applicationWillTerminate:(UIApplication *) app {
    if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)] &&
       ![[UIDevice currentDevice] isMultitaskingSupported]) {
       [[NSUserDefaults standardUserDefaults] setObject:myArray forKey:@"myArray"];
    }
}

Store the object in NSUserDefaults in -applicationWillTerminate:, if it hasn't already been saved by the invocation of -applicationDidEnterBackground: (i.e. check if multitasking is supported, if it is, then don't save it because it's already been saved.)

- (void) applicationWillTerminate:(UIApplication *) app {
    if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)] &&
       ![[UIDevice currentDevice] isMultitaskingSupported]) {
       [[NSUserDefaults standardUserDefaults] setObject:myArray forKey:@"myArray"];
    }
}
执笏见 2024-12-13 15:22:14

不要忘记在进入后台之前同步缓冲区:

[[NSUserDefaults standardUserDefaults] synchronize];

Do not forget to sync the buffer before going into background:

[[NSUserDefaults standardUserDefaults] synchronize];
毁虫ゝ 2024-12-13 15:22:14

前面的答案都是正确的,但请注意,applicationDidEnterBackgroundapplicationWillTerminate 都不能保证在所有情况下都被调用。当重要数据发生变化时,您通常最好将其存储起来。

The previous answers are all correct, but note that neither applicationDidEnterBackground nor applicationWillTerminate are guaranteed to be called in all situations. You are usually better off storing important data whenever it has changed.

白衬杉格子梦 2024-12-13 15:22:14

将 NSUserDefaults 保存在

- (void)applicationWillTerminate:(UIApplication *)application 

Save NSUserDefaults at

- (void)applicationWillTerminate:(UIApplication *)application 
明明#如月 2024-12-13 15:22:14

设置

[[NSUserDefaults standardUserDefaults] setObject:myArray forKey:@"myArray"];

applicationWillTerminate

set

[[NSUserDefaults standardUserDefaults] setObject:myArray forKey:@"myArray"];

in

applicationWillTerminate
落叶缤纷 2024-12-13 15:22:14

并且不要忘记在您尝试保存且包含在数组中的对象内使用encodeWithCoder和initWithCoder

and don't forget to use the encodeWithCoder and initWithCoder inside the object that you are trying to save and that is contained in the array

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文