首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mas OS X: contentsOfDirectoryAtURL方法中的错误

Mas OS X: contentsOfDirectoryAtURL方法中的错误
EN

Stack Overflow用户
提问于 2013-08-30 21:06:23
回答 1查看 757关注 0票数 0

我有一个问题,我有一个文件夹url,文件夹,存储在那个url路径上,并且它是存在的。问题是contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:返回时有一个错误。

我的NSURL到文件夹是NSString,它是从另一种方法生成的,该方法接受NSURL并将其保存为absoluteString对象。这是我的代码:

代码语言:javascript
复制
NSURL *folderURL = [NSURL fileURLWithPath:folderPathString isDirectory:YES];



if ([folderURL isFileURL]) {
    NSLog(@"it's file"); // I see this in console 
}


NSError *error;
// array of NSURL objects
NSArray *contentOfFolder = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:folderURL
                                                         includingPropertiesForKeys:@[NSURLContentModificationDateKey,NSURLFileResourceTypeKey, NSURLLocalizedNameKey]
                                                                            options:NSDirectoryEnumerationSkipsHiddenFiles
                                                                              error:&error];

if (error) {
    NSLog(@"%@",error);
}

这是我方法的一部分,在控制台中,我看到了一个错误:

Error Domain=NSCocoaErrorDomain Code=260 "The file “myFolder” couldn’t be opened because there is no such file." UserInfo=0x10051b0a0 {NSURL=file:/localhost/Users/myUser/myRootFolder/myFolder/ -- file://localhost/Users/myUser/Library/Developer/Xcode/DerivedData/myProject-algooymkavrtmlchwnlbrmvcbvzj/Build/Products/Debug/, NSFilePath=/Users/myUser/Library/Developer/Xcode/DerivedData/myProject-algooymkavrtmlchwnlbrmvcbvzj/Build/Products/Debug/file:/localhost/Users/myUser/myRootFolder/myFolder, NSUnderlyingError=0x100526f40 "The operation couldn’t be completed. No such file or directory"}

我不明白为什么我会犯这个错误。如果存在这个目录,我如何获得No such file or directory错误?!

编辑

我发现在方法fileURLWithPath:isDirectory:之后,当我用NSLog查看文件夹url时,它看起来很奇怪。

代码语言:javascript
复制
NSLog(@"folder url %@",folderURL);

产出:

代码语言:javascript
复制
folder url file:/localhost/Users/myUser/myRootFolder/myFolder/ 
-- file://localhost/Users/myUser/Library/Developer/Xcode/DerivedData/myProject-algooymkavrtmlchwnlbrmvcbvzj/Build/Products/Debug/

为什么第二部分会出现?(部分以-- file://localhost/Users/myUser/Library/...开头)。我觉得这有问题,但我做错了什么?方法fileURLWithPath:isDirectory:对我的目的是不可接受的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-31 02:38:30

中的folderPathString

代码语言:javascript
复制
NSURL *folderURL = [NSURL fileURLWithPath:folderPathString isDirectory:YES];

必须是一个简单的路径,例如"/ path /to/dir“。在您的例子中,它是一个字符串URL "file://localhost/path/to/dir",,这是错误的。

我假设folderPathString是从一些NSURL创建的

代码语言:javascript
复制
folderPathString = [anURL absoluteString];

这是错误的,应该是

代码语言:javascript
复制
folderPathString = [anURL path];

还可以避免从URL转换到字符串,然后完全返回到URL。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18541470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档