Mil*_*0R3 9 macos cocoa objective-c
我想让用户选择保存文件的目录.但是如何确保url是一个目录而不是一个文件?
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
}
}
}];
Run Code Online (Sandbox Code Playgroud)
Ken*_*det 23
未来阅读此内容的任何人的更新:
在Swift中,可以通过使用来检查选择的路径是否是文件
panel.canChooseFiles = false
Run Code Online (Sandbox Code Playgroud)
// 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
}
}
Run Code Online (Sandbox Code Playgroud)