NSOpenPanel choose a directory ( not a file)
cocoa, macos, objective-c
Solution
// First, check if the URL is a file URL, as opposed to a web address, etc.
if (url.isFileURL) {
BOOL isDir = NO;
// Verify that the file exists
// and is indeed a directory (isDirectory is an out parameter)
if ([[NSFileManager defaultManager] fileExistsAtPath: url.path isDirectory: &isDir]
&& isDir) {
// Here you can be certain the url exists and is a directory
}
}
Problem
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 } } }]; ```