xcode 在 iPhone 中保存会话数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5479358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save Session Data in iPhone
提问by developer
I want to store data at different points in my app, that is accessible to objects across the app. Something similar to session in php or global variables. I know I can use NSUserDefaults but i am not sure how i would keep on adding values to it and then accessing it. Example, first i want to store the Username that is used during login, then on 3rd screen, i want to save the Company of that user that he selects from a list of companies. Then on the 5th screen i want to save the location that the user selects. And then I have different functionalities that the user can use depending on his selections. As I am a newbie i am sorry if this is a dumb question but can anybody help me as to how i would store and retrieve mulitple data that is accessible throughout the app.
我想在我的应用程序中的不同点存储数据,以便应用程序中的对象可以访问这些数据。类似于 php 中的 session 或全局变量。我知道我可以使用 NSUserDefaults,但我不确定如何继续向它添加值然后访问它。例如,首先我想存储登录期间使用的用户名,然后在第三个屏幕上,我想保存他从公司列表中选择的该用户的公司。然后在第 5 个屏幕上,我想保存用户选择的位置。然后我有不同的功能,用户可以根据他的选择使用这些功能。由于我是新手,如果这是一个愚蠢的问题,我很抱歉,但有人可以帮助我了解如何存储和检索可在整个应用程序中访问的多个数据。
回答by Gregorius T
On iOS devices you have various options for storing data. Here are a few you might consider:
在 iOS 设备上,您有多种存储数据的选项。以下是您可能会考虑的一些:
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.
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.
Store data to text files. I'm not sure if this has any advantage over property lists.
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.
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.
Core Data 和 SQL 数据存储库或 XML 存储库。这基本上是 Apple 的设备数据库框架库,可供所有应用程序使用。它不是最简单的选项,但它允许您存储相当大量的不同数据,这些数据将在整个应用程序代码中可用。此外,只要您在应用程序关闭之前将数据保存到持久性数据存储,数据就会在应用程序启动之间保留。
财产清单。属性列表比 Core Data 更轻量级,您可能会发现它们更易于使用。它们允许将基本键值对存储和检索到持久属性列表文件中。因此,只要您在应用程序关闭之前将数据存储到属性列表文件中,您还可以获得应用程序启动之间数据保留的优势。
将数据存储到文本文件。我不确定这是否比属性列表有任何优势。
用户默认值。我不确定您是否可以在应用程序使用期间轻松添加新类型的数据。这更多地用于提供默认应用程序设置的集合,然后允许用户在应用程序使用期间对其进行个性化设置。
内存中的单例对象。这可能是一种选择,但是,当然,一旦应用程序关闭,所有数据都会消失,除非以某种方式将其持久化到永久数据存储中。
I'm sure there are other options in addition to these. I will be interested to read about them. I hope this helps.
除了这些,我相信还有其他选择。我将有兴趣阅读有关它们的信息。我希望这有帮助。
回答by Gregorius T
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!
您在评论中要求我提供有关属性列表的代码片段。当然。没问题。您需要做的第一件事是创建您的属性列表文件。只需将一个属性列表类型的文件添加到您的项目中。我们称之为 DataPoints.plist。出于本示例的目的,该文件将包含 NSString 类型的键值条目。在代码示例中,我只是提取属性列表条目并加载数组。现在你可以对数组做任何你想做的事情。使用它来加载表格视图或其他任何东西。希望这可以帮助。如果是这样,您能否将其标记为已接受的答案。谢谢!
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"]];
回答by Joris Mans
create a singleton object. I know it is not a very nice pattern but it is the easiest solution to your question.
创建一个单例对象。我知道这不是一个很好的模式,但它是解决您问题的最简单方法。