退出时保存应用程序数据状态 [英] Saving application data state on exit

查看:135
本文介绍了退出时保存应用程序数据状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含24个字符串的NSMutableArray。

I have an NSMutableArray with 24 strings.

如果用户接到电话或退出应用程序,我需要保存这些数据。

I need to save this data if a user gets a call or quits the application.

我一直在研究很多例子但由于某些原因似乎无法确定保存数据的最佳方法。

I have been looking into many examples but for some reason can’t seem to determine the best way to save the data.

24个字符串对应24个按钮及其状态。单击按钮时,将显示该按钮标记的相应阵列信息(0 - 23)。我需要保留的是,如果点击了10个按钮并显示了他们的数据,那么保留这些数据的最佳方式是什么/什么是什么,以便在应用程序启动时重新加载?

The 24 strings correspond to 24 buttons and their state. When a button is clicked, it displays the corresponding Array info for that buttons tag (0 – 23). What I would need to retain is if 10 buttons where clicked and their data shown, how/what would be the best way of retaining this data so it can be reloaded when the app starts?

我想我需要存储:

按钮标记,
按钮对应的数组值,

按钮状态(是否已点击,值是否显示)

Button Tag, Buttons corresponding Array value,
Button state (whether it has clicked and value is show or not)

我会在退出应用程序时存储此数据,然后何时应用程序再次启动,我将确定此存储的数据是否存在,如果是这样填充数组并检查按钮状态以确定它是否已经显示,如果是,则相应地进行设置。然后,当加载此文件时,我将删除存储的数据(如果以这种方式存储,则为.DAT文件)。这样,如果用户正常退出,则在下次启动时,它将启动一个新游戏。

I would store this data on exit of the application and then when app is started again, I would determine if this stored data exists, if so populate the array and examine the button states to determine if it had already been shown and if so, set it accordingly. Then when this file was loaded, I would delete the stored data (.DAT file if stored this way). This way if a user quits gracefully, on next start up, it would start a new game.

我已经查看了几个将数据存储到.DAT文件中的示例但我实施这个问题......并且想知道这是否是最好的方式。

I have looked at several examples where they store data into a .DAT file but am having problem implementing this….and wondering if this is even the best way.

非常感谢任何帮助或想法。

Any help or thoughts on this is greatly appreciated.

Geo ...

推荐答案

您可以将数据保存到Documents目录中的plist。如果数据在那里,加载它,如果没有,它会建议一个干净的运行。

You could save the data to a plist in the Documents directory. If the data is there, load it up, and if not, it would suggest a clean run.

加载数据:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [paths objectAtIndex:0];
NSString *filePath = [documents stringByAppendingPathComponent:@"buttonState.plist"];
NSMutableDictionary *buttonState = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 

保存数据:

[buttonState writeToFile:filePath atomically: YES]; 

这篇关于退出时保存应用程序数据状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆