退出+ objective c后获取数组中的值

时间:2011-05-05 06:45:25

标签: iphone objective-c ios persistence

在我的iphone应用程序中,我将值存储在数组中,我想在重新加载应用程序时检索它。也就是说,当用户退出我的应用程序时,我希望保存此数组,以便在加载应用程序时再次被用户想要访问。

3 个答案:

答案 0 :(得分:0)

您可以使用UIApplicationDelegate方法applicationWillTerminateapplicationDidEnterBackground来保存您的数据。如果您是从模板创建项目,则应在appDelegate中使用一些注释来实现它们。

答案 1 :(得分:0)

- (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, called instead of applicationWillTerminate: when the user quits.
     */
// You can save your array here
}

- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
    //Save array here
}

答案 2 :(得分:0)

为了补充以前的答案,您可以使用KeyedArchiver读取/写入您的对象文件。

BOOL result = [NSKeyedArchiver archiveRootObject:your_NSArray toFile:filePath];

id archive = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; 

可以通过

访问应用程序可以存储文件的标准iOS目录
NSSearchPathForDirectoriesInDomains