我需要存储预先配置好的值,这些值稍后将用于用户可以从iOS应用程序的表中选择的选项,可能是5-10个选项。
在性能和效率方面,存储这类数据的最佳方法是什么?
我可能会想到几种方法,比如:
谢谢
发布于 2012-08-06 07:30:33
如果它实际上只是5到10个数据项,那么您可以将它们存储在一个NSDictionary或数组中,并将其保存在一个plist文件中并从中读取。您可以分别使用dictionaryWithContentsOfFile方法或arrayWithContentsOfFile从plist中读取数据,并使用writeToFile编写。
对于大量的数据,您可以查看核心数据。
发布于 2012-08-08 03:07:45
静态数据应存储在相应类的静态变量中,这些变量将加载这些值。
不管您是否从文件中加载字典,这里都是如何静态地加载字典或数组,因此它在应用程序中只加载一次。
//.h
@interface MyApp
+(void) initialize; //will only be called once when the class is loaded
//.m
static NSArray *myListOfStuff;
@implementation MyApp
+(void) initialize {
//...either load your values from a file or hard code the values here
//init and assign values to myListOfStuff
}
//a statis getter for the list
+(NSArray *) listOfStuff {
return myListOfStuff;
}
//Client Code to get the list in your app
NSArray *myList = [MyApp listOfStuff];
//This memory will not be released for the life of the application.
//it will be loaded once and only once - its efficientGoogle将字典或数组持久化到plist --如果持久化是您所追求的--我向您展示的是,不管持久化方法如何,如何高效地构造在对象模型中加载静态数据
https://stackoverflow.com/questions/11823967
复制相似问题