在 iOS 中保存数据的选项有哪些?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4075056/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:00:09  来源:igfitidea点击:

What are the options for saving data in iOS?

iosobjective-c

提问by Joey

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 NSSearchPathForDirectoriesInDomainsfor finding the appropriate storage directory root (Documents?) and then use NSDatato 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?

我收集到我应该使用 NSSearchPathForDirectoriesInDomains来查找适当的存储目录根(文档?),然后使用NSData来存储我创建的这些数据缓冲区并将它们写入文件。我是在看还是我弄错了?任何其他贤者的建议?

回答by Beno?t

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。
在不使用核心数据的情况下保存字符串列表或其他数据非常容易。
请参阅属性列表编程指南

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)

支持的类有一些限制(请参阅指南)

回答by zoul

It depends on the data you are writing. If you mostly want to serialize objects, see the NSCodingprotocol. If you have some simple data structure that's not a class, you can store that into a?NSDictionaryand 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 NSDataas you already hinted. In any case be sure to read the Archives and Serializations Programming Guideso that you don't reinvent the wheel.

这取决于您正在写入的数据。如果您主要想序列化对象,请参阅NSCoding协议。如果您有一些不是类的简单数据结构,您可以将其存储到一个? NSDictionary并将其保存到 plist 中。如果你有一大块数据(比如数千个浮点数或类似的东西),你可能想像NSData你已经暗示的那样保存它。在任何情况下,请务必阅读存档和序列化编程指南,以免重新发明轮子。

回答by Nava Carmon

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 上保存文件