NSOpenPanel choose a directory ( not a file)

Viewed 7353

I want to let user to choose a directory for saving a file. but how to make sure the url is a directory not a file?

NSOpenPanel* panel = [NSOpenPanel openPanel];
[panel setCanChooseDirectories:YES];
[panel setCanCreateDirectories:YES];

[panel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){
    if (result == NSFileHandlingPanelOKButton) {
        NSArray* urls = [panel URLs];
        for (NSURL *url in urls) {
            //here how to judge the url is a directory or a file
        }
    }
}];
3 Answers

Swift 4 version:

if (url.isFileURL) {
    var isDir: ObjCBool = false
    if (FileManager.default.fileExists(atPath: url.path, isDirectory: &isDir)) { 
       if isDir.boolValue {
                            print("It's a Directory path")
                          } else {
                            print("It's a file path")
                        }
                    }
}
Related