正确使用NSSearchPathForDirectoriesInDomains [英] using NSSearchPathForDirectoriesInDomains properly

查看:56
本文介绍了正确使用NSSearchPathForDirectoriesInDomains的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对存档我的对象还很陌生.我有一本要存档的字典..因此首先我通过以下操作获得了路径:

I am fairly new to archiving an object I have. I have a dictionary that I want to archive.. so first I got the path by doing:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        dictionaryPath = [paths objectAtIndex:0];

下一步是在字典路径后附加.txt吗?还是应该首先在此目录中创建一些内容?那么数据类型是什么?

so is the next step to append this dictionaryPath with a .txt? Or should I create something first at this directory? What is then the data type?

我希望能够将其用于:

[NSKeyedArchiver archiveRootObject:locationDic toFile:dictionaryPath];

dictionaryPath是存储此NSDictionary的路径

Where dictionaryPath is the path to store this NSDictionary

推荐答案

NSSearchPathForDirectoriesInDomains将为您提供一系列目录路径.因此,如果您选择第一个文件,则需要附加一个文件名(无论您要使用什么文件名,并使用所需的扩展名),然后使用该路径作为写入的位置.

NSSearchPathForDirectoriesInDomains will give you an array of directory paths. So if you take the first one, you need to append a filename (whatever filename you'd like, with whatever extension you'd like), and then use that path as the location to write out to.

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *dictionaryFilePath = [documentsDirectory stringByAppendingPathComponent:@"Filename.extension"];
 [NSKeyedArchiver archiveRootObject:locationDic toFile:dictionaryFilePath];

这篇关于正确使用NSSearchPathForDirectoriesInDomains的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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