简体   繁体   English

目标C iPhone开发,保存和检索NSMutableArray

[英]Objective C iPhone Development, saving and retrieving an NSMutableArray

I am new to iPhone development and I am using XCode 4.2 I am trying to save an NSMutable array so that when I close the application and reopen it , the data will still be there I am using these two functions : 我是iPhone开发的新手,我正在使用XCode 4.2,我试图保存一个NSMutable数组,以便当我关闭应用程序并重新打开它时,数据仍然存在,我正在使用以下两个功能:

-(void)saveData{
    [NSKeyedArchiver archiveRootObject:[data copy] toFile:[self dataFilePath]];
}
- (void)loadData
{
    data = [NSKeyedUnarchiver unarchiveObjectWithFile:self.dataFilePath];
}

but I found two major issues : 1- when I switch views and come back , the data is gone 2- when I completely exit the application (ie I double click on the Main button and remove it from the list of running application) the data is not erased as well 但是我发现了两个主要问题:1-当我切换视图并返回时,数据消失了; 2-当我完全退出该应用程序时(即,双击Main按钮并将其从正在运行的应用程序列表中删除)也不会被擦除

I found that I can use these two NSMutableArray methods : writeToFile and initWithContentsOfFile but I don t know where to call them (is it when I add items to the UITableView ? in the viewDidLoad ? 我发现我可以用这两种方法的NSMutableArray: writeToFileinitWithContentsOfFile ,但我不知道在哪里可以打电话给他们(是,当我将项目添加到UITableView中的? viewDidLoad

can somebody give me a sample code? 有人可以给我示例代码吗?

Thanks in advance 提前致谢

For saving an array use writeToFile:atomically: method. 要保存数组,请使用writeToFile:atomically:方法。 It will save your NSArray content as plist file. 它将您的NSArray内容另存为plist文件。 For load an array from plist file, use initWithContentsOfFile: method. 要从plist文件加载数组,请使用initWithContentsOfFile:方法。

Please note, that these methods will be able to save your stuff, only if your array has the following class instances NSString , NSData , NSDate , NSNumber NSArray , or NSDictionary as items. 请注意,仅当您的数组具有以下类实例NSStringNSDataNSDateNSNumber NSArrayNSDictionary作为项目时,这些方法才能保存您的东西。

you can call save/load methods like following 您可以调用如下的保存/加载方法

// in view controller implementation

- (void) saveData {
  [self.theArray writeToFile: self.filePath automatically:NO];
}

- (void) loadData {
  self.theArray = [NSArray arrayWithContentsOfFile: self.filePath];
}

- (void) viewDidLoad {
  [super viewDidLoad];

  [self loadData];
}

- (void) viewWillDisappear: (BOOL) animated {
  [super viewWillDisappear: animated];

  [self saveData];
}


// in the UIApplicationDelegate implementation
- (void)applicationWillResignActive:(UIApplication *)application {
  [myCustomController saveData];
}

The problem is that when those 2 actions happen, saveData is not getting called. 问题是当这两个动作发生时,不会调用saveData。 Therefore I would suggest saving your data on the AppDelegate on this method to solve problem 2 (exit app) 因此,我建议您使用此方法将数据保存在AppDelegate上,以解决问题2(退出应用)

- (void)applicationDidEnterBackground:(UIApplication *)application
{
//Save data
}

As for problem 1 (when switching views), you just need to call your saveData on this method on your viewController: 至于问题1(切换视图时),您只需要在viewController的此方法上调用saveData即可:

- (void)viewWillDisappear:(BOOL)animated
{
   //Save data
}

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM