我有一个问题,我有一个文件夹url,文件夹,存储在那个url路径上,并且它是存在的。问题是contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:返回时有一个错误。
我的NSURL到文件夹是NSString,它是从另一种方法生成的,该方法接受NSURL并将其保存为absoluteString对象。这是我的代码:
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时,它看起来很奇怪。
NSLog(@"folder url %@",folderURL);产出:
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:对我的目的是不可接受的吗?
发布于 2013-08-31 02:38:30
中的folderPathString
NSURL *folderURL = [NSURL fileURLWithPath:folderPathString isDirectory:YES];必须是一个简单的路径,例如"/ path /to/dir“。在您的例子中,它是一个字符串URL "file://localhost/path/to/dir",,这是错误的。
我假设folderPathString是从一些NSURL创建的
folderPathString = [anURL absoluteString];这是错误的,应该是
folderPathString = [anURL path];还可以避免从URL转换到字符串,然后完全返回到URL。
https://stackoverflow.com/questions/18541470
复制相似问题