Nikolozi
Nikolozi

Reputation: 2242

NSFileManager finds files inside a folder only when it's running under a debugger

When I run the following code under the Xcode debugger it successfully finds the package with .app extension, but when I run it standalone "file" object is nil. In fact when I did NSLogs folderEnum was also nil. Note that folderPath points to a folder that is in the same directory as the the program executable.

NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSDirectoryEnumerator *folderEnum = [localFileManager enumeratorAtPath:folderPath];
NSString *file;
while (file = [folderEnum nextObject]) {
  if ([[file pathExtension] isEqualToString: @"app"]) {
     break;
  }
}

Any ideas? Something to do with the Mac system file permissions?

Edit

I should have probably mentioned that folderPath was actually a relative path and not an absolute one. So I changed folderPath to be relative to [[NSBundle mainBundle] bundlePath] path and it works now. But if anyone can shed some light why relative path doesn't work that be great.

Upvotes: 0

Views: 344

Answers (1)

Francis McGrew
Francis McGrew

Reputation: 7272

Does changing the first line to:

NSFileManager *localFileManager = [NSFileManager defaultManager];

make any difference? Are you just trying to get the path for your application? (There are easier ways)

Upvotes: 1

Related Questions