在iOS中保存数据的选项有哪些? [英] What are the options for saving data in iOS?

查看:108
本文介绍了在iOS中保存数据的选项有哪些?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想序列化一堆数据并将其保存到文件中,然后能够(自然地)加载它并使用我编写的功能播放它。 (对不起,如果我的术语没有了,我就是这样的新手。)使用iOS的最佳方法是什么?从这里查看文档:

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:

标准应用行为指南

I我已经聚集起来,我应该使用
NSSearchPathForDirectoriesInDomains ,用于查找相应的存储目录根目录(Documents?),然后使用 NSData 存储我创建的这些数据缓冲区并将它们写入文件。我有没有发现或者我弄错了?还有其他的圣人建议吗?

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?

推荐答案

你可以使用plist。

保存列表很容易不使用核心数据的字符串或其他数据。

请参阅属性列表编程指南

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

例如,此代码将保存一个对象( rootObj)可以是数组或字典:

For example, this code will save an object (rootObj) which can be an array or a dictionary:

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)

这篇关于在iOS中保存数据的选项有哪些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆