在我的应用程序中保存少量数据时,使用NSUserDefaults或NSCoding更好吗?现在我使用NSCoding (encodeWithCoder/initWithCoder等)但看起来NSUserDefaults可能更简单。我的总数据是关于各种Ints/Strings/MutableArray的,只有几十个总数。
发布于 2014-04-11 17:26:11
我假设NSCoding的意思是“在用NSCoding API序列化对象之后将对象保存到文件中”。虽然这两种方法对于原始数据类型都是有效的,但是一旦您开始使用复杂的结构序列化对象,NSUserDefaults方法就会变得更加困难。
相反,将NSCoding类的数据保存到文件中,在对象结构方面提供了高度的灵活性。如果您知道将来不需要这种灵活性,请使用NSUserDefaults;如果您不确定,请继续处理这些文件。
发布于 2014-04-11 17:26:39
我倾向于使用以编程方式创建的plist文件。
NSString *appFile;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
appFile = [documentsDirectory stringByAppendingPathComponent:@"myFile"];
//this creates the file path
NSDictionary* tempDict = [[NSDictionary alloc]initWithContentsOfFile:appFile];
//this gets the data from storage
[tempDict writeToFile:appFile atomically:YES];
//this updates the data to storagehttps://stackoverflow.com/questions/23018817
复制相似问题