iPh*_*her 9 iphone cocoa-touch
是NSSearchPathForDirectoriesInDomain S功能仍得到iPhone的文件目录的路径的最佳方式?我问,因为我在去年看到的大多数主题都是过去的日期,而且它似乎仍然是一种非常繁琐的方式来访问iPhone上常用的目录.你认为现在有一个方便的方法,类似于NSBundle的bundlePath,executablePath等.
为了清楚起见,这意味着调用"NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)"并返回索引为0的Documents路径的数组.
Dan*_*pos 22
Xcode中基于核心数据的应用程序模板提供了以下方法:
- (NSString *)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
Run Code Online (Sandbox Code Playgroud)
因此,Apple似乎仍然支持以这种方式获取文档目录.我想,你可以把它放到一个类别中,但是我发现它足以将该方法包含在给定应用程序中需要在文档目录中工作的少数几个类中.如果你在整个地方做了很多文件,你可以考虑重构一下你的代码,将这些任务限制在一个或两个管理器类中.
对我来说,至少,第三次或第四次我说"嘿,让文档目录是一个痛苦的脖子"是我意识到有机会将文件转换成一个专门的类.
Iva*_*vic 17
Xcode中当前的Core Data iOS应用程序模板提供了以下方法:
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
Run Code Online (Sandbox Code Playgroud)
这对我有用,非常短而甜蜜
#define kDOCSFOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
Run Code Online (Sandbox Code Playgroud)
干杯!
| 归档时间: |
|
| 查看次数: |
14799 次 |
| 最近记录: |