我有一个麻烦,我有一个文件夹 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:
不适合我的目的吗?