NSDirectoryEnumerator returns nil

I'm trying to display my images in a tableView, I'm using NSFIleManager and NSDirectoryEnumerator to get all files in the current folder:

  NSString *path = @"/Users/eagle/Documents/avatars";
    NSFileManager *fileManager = NSFileManager.defaultManager;
    NSDirectoryEnumerator *directoryEnum = [fileManager enumeratorAtPath:path];
    
    NSString *file;
    while (file = [directoryEnum nextObject])
    {
          // ... 
    }

the problem is that this line

file = [directoryEnum nextObject]

always returns nil, what gives?

I already made sure that this folder has no subfolders and contains only images, so what's the problem here?

Answered by DTS Engineer in 807999022

The answer depends a little bit on what you're actually trying to achieve.

  • You can use NSOpenPanel to ask the user to "confirm" choice of a directory, thne save the result as a security-scoped bookmark to maintain access in future launches of your app. You can let the user choose or create the folder, or — by manipulating various NSOpenPanel settings — you can restrict the user to choosing only a specific folder.

  • You can use a standard folder that's freely available to your sandboxed app's container. A likely choice, if you storing avatars, which may be sort-of persistent app-level data, is to put them in a sub-folder of the Application Support folder. The usual convention is to use your app's name or its bundle ID as the sub-folder name.

Okay, I found the issue, I'm supposed to remove the App sandboxing in the "Signings & Capabilities"

Before I close this question I'd like to know if there's any way of making this work without disabling the sandbox

Accepted Answer

The answer depends a little bit on what you're actually trying to achieve.

  • You can use NSOpenPanel to ask the user to "confirm" choice of a directory, thne save the result as a security-scoped bookmark to maintain access in future launches of your app. You can let the user choose or create the folder, or — by manipulating various NSOpenPanel settings — you can restrict the user to choosing only a specific folder.

  • You can use a standard folder that's freely available to your sandboxed app's container. A likely choice, if you storing avatars, which may be sort-of persistent app-level data, is to put them in a sub-folder of the Application Support folder. The usual convention is to use your app's name or its bundle ID as the sub-folder name.

Also, the enumerator mechanism is kinda out old school. A better way to enumerate a directory is with the -contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: method. This has a couple of important advantages:

  • The keys parameter lets you prepopulate the properties you need on each URL. This is really big performance boost if you’re working with a high-latency volume, like one mounted over the network.

  • It gives you a useful error if it fails.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

NSDirectoryEnumerator returns nil
 
 
Q