Save Session Data in iPhone

被刻印的时光 ゝ 提交于 2019-12-04 16:50:41

You asked me in your comment to provide a code snippet on property lists. Sure. No prob. First thing you need to do is create your property list file. Just add a file of type property list to your project. Let's call it DataPoints.plist. For purposes of this example, the file will contain key-value entries of type NSString. In the code example, I'm simply extracting the property list entries and loading up an array. Now you can do whatever you want with the array. Use it to load up a table view or whatever. Hope this helps. If it did, can you please mark it as the accepted answer. Thanks!

NSString *errorDesc;
NSPropertyListFormat format;
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"DataPoints" ofType:@"plist"];

NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];

NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
                                     propertyListFromData:plistXML
                             mutabilityOption:NSPropertyListMutableContainersAndLeaves
                             format:&format
                             errorDescription:&errorDesc];

if (!temp) {
     // Handle Error
     Log(@"Error reading plist: %@, format: %d", errorDesc, format);
}

NSArray *dataPoints = [NSArray arrayWithArray:[temp objectForKey:@"DataPoints"]];

On iOS devices you have various options for storing data. Here are a few you might consider:

  1. Core Data and either a SQL data repository or an XML repository. This is basically Apple's device database framework library available for all apps to use. It is not the easiest of options but it will allow you to store fairly large amounts of diverse data that will be available throughout your app code. In addition, the data is retained between app launches as long as you save it to the persistent data store before app shut down.

  2. Property lists. Property lists are more lightweight than Core Data and you might find them easier to use. They allow storage and retrieval of basic key-value pairs into a persistent property list file. So, you also get the advantage of data retention between app launches as long as you store your data to the property list file before app shut down.

  3. Store data to text files. I'm not sure if this has any advantage over property lists.

  4. User Defaults. I'm not sure that you can so easily add new types of data during app usage. This is used more for providing a collection of default app settings and then allowing the user to personalize them during app usage.

  5. In-memory singleton objects. This could be an option but, of course, once the app shuts down, all data goes away unless it's persisted into permanent data storage somehow.

I'm sure there are other options in addition to these. I will be interested to read about them. I hope this helps.

create a singleton object. I know it is not a very nice pattern but it is the easiest solution to your question.

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