I'd like to serialize a bunch of data and save it to a file, then be able to load it (naturally) and play it back with a feature I have written. (Sorry if my terminology is off, I am a novice with this sort of thing.) What is the best way to do this with iOS? From looking at documentation here:
我想序列化一堆数据并将其保存到一个文件中,然后能够(自然地)加载它,并使用我编写的特性回放它。(对不起,如果我的术语不正确,我是这类事情的新手。)使用iOS实现这一点的最佳方式是什么?从这里查看文档:
Standard Application Behaviors Guide
标准应用程序行为指南
I've gathered that I should use NSSearchPathForDirectoriesInDomains for finding the appropriate storage directory root (Documents?) and then use NSData to store these data buffers I create and write them to file. Am I spot on with that or have I got it wrong? Any other sage advice?
我已经收集到,我应该使用nssearchpathfordirectoriesindomain来查找适当的存储目录根(文档?),然后使用NSData来存储我创建的数据缓冲区并将它们写入文件。我是有这个想法还是我弄错了?其他明智的建议吗?
3 个解决方案
#1
18
You can use plist.
It is very easy to save list of string or other data without using core data.
See Property List Programming Guide
您可以使用plist。在不使用core data的情况下,很容易保存字符串或其他数据的列表。参见属性列表编程指南
For example, this code will save an object (rootObj) which can be an array or a dictionary:
例如,此代码将保存一个对象(rootObj),该对象可以是数组或字典:
NSString *error;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"yourFile.plist"];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:rootObj
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
if(plistData) {
[plistData writeToFile:plistPath atomically:YES];
}
else {
NSLog(@"Error : %@",error);
[error release];
}
There is some limitation on supported class (see the guide)
支持类有一些限制(参见指南)
#2
14
It depends on the data you are writing. If you mostly want to serialize objects, see the NSCoding protocol. If you have some simple data structure that’s not a class, you can store that into a NSDictionary
and save it into a plist. And if you have a big chunk of data (like thousands of floats or something like that), you might want to save that using NSData
as you already hinted. In any case be sure to read the Archives and Serializations Programming Guide so that you don’t reinvent the wheel.
这取决于你写的数据。如果您主要希望序列化对象,请参见NSCoding协议。如果你有一些不属于类的简单数据结构,你可以将它存储到NSDictionary中并保存到plist中。如果你有大量的数据(比如成千上万的浮点数或类似的数据),你可能会想用NSData来保存,就像你已经暗示的那样。在任何情况下,一定要阅读存档和序列化编程指南,这样你就不会重新发明*。
#3
1
Yes, pretty right direction. You need to implement something, that is called NSCoding protocol on your object to be able to archive & unarchive it into NSData. See the example: Saving files on iOS using NSFileManager
是的,很正确的方向。您需要实现一些东西,在对象上称为NSCoding协议,以便能够将其归档和反归档到NSData中。请参见示例:使用NSFileManager在iOS上保存文件
#1
18
You can use plist.
It is very easy to save list of string or other data without using core data.
See Property List Programming Guide
您可以使用plist。在不使用core data的情况下,很容易保存字符串或其他数据的列表。参见属性列表编程指南
For example, this code will save an object (rootObj) which can be an array or a dictionary:
例如,此代码将保存一个对象(rootObj),该对象可以是数组或字典:
NSString *error;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"yourFile.plist"];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:rootObj
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
if(plistData) {
[plistData writeToFile:plistPath atomically:YES];
}
else {
NSLog(@"Error : %@",error);
[error release];
}
There is some limitation on supported class (see the guide)
支持类有一些限制(参见指南)
#2
14
It depends on the data you are writing. If you mostly want to serialize objects, see the NSCoding protocol. If you have some simple data structure that’s not a class, you can store that into a NSDictionary
and save it into a plist. And if you have a big chunk of data (like thousands of floats or something like that), you might want to save that using NSData
as you already hinted. In any case be sure to read the Archives and Serializations Programming Guide so that you don’t reinvent the wheel.
这取决于你写的数据。如果您主要希望序列化对象,请参见NSCoding协议。如果你有一些不属于类的简单数据结构,你可以将它存储到NSDictionary中并保存到plist中。如果你有大量的数据(比如成千上万的浮点数或类似的数据),你可能会想用NSData来保存,就像你已经暗示的那样。在任何情况下,一定要阅读存档和序列化编程指南,这样你就不会重新发明*。
#3
1
Yes, pretty right direction. You need to implement something, that is called NSCoding protocol on your object to be able to archive & unarchive it into NSData. See the example: Saving files on iOS using NSFileManager
是的,很正确的方向。您需要实现一些东西,在对象上称为NSCoding协议,以便能够将其归档和反归档到NSData中。请参见示例:使用NSFileManager在iOS上保存文件