如何获取文档目录中特定目录的URL路径。
喜欢Document/Art/
我的代码
- (NSURL *)localRoot {
if (_localRoot != nil) {
return _localRoot;
}
NSArray * paths = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
_localRoot = [paths objectAtIndex:0];
return _localRoot;
}
以上代码是Documents
目录的url路径,但我需要Art
文档目录路径。
答案 0 :(得分:9)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *yourArtPath = [documentsDirectory stringByAppendingPathComponent:@"/Art"];
答案 1 :(得分:1)
使用此
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile_a = [documentsDirectory stringByAppendingPathComponent:@"/Art"];
答案 2 :(得分:0)
<强> Application.StartupPath; 强>
用于查找启动应用程序的路径,如果这是您的意思。
答案 3 :(得分:0)
基于上面的答案,这里是一个简单的方法,返回给定目录的路径:
+ (NSString *) pathFor : (NSString *) directoryName{
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:directoryName];
}
您需要在ClassName.m
中实现它并调用:
NSString *pathForDirectory = [ClassName pathFor:dirName]; //@"Art" in your case