获取iPhone文档目录。是NSSearchPathForDirectoriesInDomains仍然是唯一的办法吗? [英] Getting iPhone Documents Directory. Is NSSearchPathForDirectoriesInDomains still the only way?

查看:88
本文介绍了获取iPhone文档目录。是NSSearchPathForDirectoriesInDomains仍然是唯一的办法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSSearchPathForDirectoriesInDomain 的功能仍然是获取iPhone文档目录路径的最佳方式?我问,因为我看到的大多数主题是去年的日期,它仍然是一个非常麻烦的方式到一个常用的iPhone目录。你会认为现在有一个方便的方法,类似于NSBundle的bundlePath , executablePath 等。

Is the NSSearchPathForDirectoriesInDomains function still the best way to get the path of the iPhone Documents directory? I ask because most topics I see on this are dated last year, and it still seems like a pretty cumbersome way of getting to a directory that is used commonly on iPhones. You'd think that there'd be a convenience method for this by now, similar to NSBundle's bundlePath, executablePath, etc.

为了清楚起见,这意味着调用NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES),并返回一个数组,文档路径在索引0处。

Just to be clear, this means calling "NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)" and getting back an array with the Documents path at index 0.

推荐答案

Xcode中基于Core Data的应用程序模板提供了以下方法:

The Core Data-based application template in Xcode provides this method:

- (NSString *)applicationDocumentsDirectory {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}

所以看起来苹果继续支持以这种方式获取文档目录。你可以把它放入一个类,我想,但我发现足够包括该方法在一小部分的类,在给定的应用程序,需要在文档目录中工作。如果你在整个地方做了很多文件,你可能会考虑重构你的代码,以限制这些任务到一两个经理类。

So it would seem that Apple continues to endorse getting the documents directory that way. You could put it into a category, I suppose, but I have found it is enough to include that method in the small handful of classes in a given app that need to do work in the documents directory. If you're doing a lot of file stuff all over the place, you might consider refactoring your code a bit to confine those tasks to one or two manager classes.

我,至少,第三或第四次我说获取docs目录是一个痛苦的脖子是我意识到一些机会,将文件转换为一个专门的类。

For me, at least, the third or fourth time I said "Hey, getting the docs directory is a pain in the neck" was the point where I realized some opportunities to shift the file juggling into a dedicated class.

这篇关于获取iPhone文档目录。是NSSearchPathForDirectoriesInDomains仍然是唯一的办法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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