getting values in an array after exiting+objective c

我的未来我决定 提交于 2019-12-12 02:13:40

问题


In my iphone application I am storing values in an array and I want to retrieve it when I reload the application.That is,When the user exits my app, I want this array to be saved so that when the app is loaded again by the user it want to be accessed.


回答1:


You can make use of the UIApplicationDelegate methods applicationWillTerminate and applicationDidEnterBackground to save your data. If you created your project from a template they should be implemented with some comments in your appDelegate.




回答2:


- (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
}



回答3:


To complement previous answers, you can use KeyedArchiver to read / write your object to file.

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

and

id archive = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; 

Standard iOS directories where your application can store files are accessed through

NSSearchPathForDirectoriesInDomains


来源:https://stackoverflow.com/questions/5893694/getting-values-in-an-array-after-exitingobjective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!