Dima Deplov
Dima Deplov

Reputation: 3718

Mas OS X: error in contentsOfDirectoryAtURL method

I have a trouble, I have a folder url, folder, that stored on that url path is exist and it's ok. Problem is that contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: returns with an error.

My NSURL to folder is NSString, that made from another method that take NSURL and save it as absoluteString object. Here is my code:

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);
}

This is a part of my method, in console, I see an 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"}

I don't understand why I get this error. How I can get No such file or directory error, if this directory exist?!

EDIT

I found that after method fileURLWithPath:isDirectory: my folder url looks strange, when I look at it with NSLog.

NSLog(@"folder url %@",folderURL);

output:

folder url file:/localhost/Users/myUser/myRootFolder/myFolder/ 
-- file://localhost/Users/myUser/Library/Developer/Xcode/DerivedData/myProject-algooymkavrtmlchwnlbrmvcbvzj/Build/Products/Debug/

Why the second part is appear? (part that starts with -- file://localhost/Users/myUser/Library/...). I think problem with this but what I do wrong? Is method fileURLWithPath:isDirectory: don't acceptable for my purposes?

Upvotes: 0

Views: 770

Answers (1)

Martin R
Martin R

Reputation: 539775

The folderPathString in

NSURL *folderURL = [NSURL fileURLWithPath:folderPathString isDirectory:YES];

must be a simple path, e.g. "/path/to/dir". In your case, it is a string URL "file://localhost/path/to/dir", which is wrong.

I assume that folderPathString is created from some NSURL using

folderPathString = [anURL absoluteString];

This is wrong and should be

folderPathString = [anURL path];

It might also be possible to avoid the conversion from URL to string and back to URL altogether.

Upvotes: 1

Related Questions