11

ファイルを保存するディレクトリをユーザーに選択させたい。しかし、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
        }
    }
}];
4

3 に答える 3

27

今後これを読む人のための更新:

Swift では、選択したパスがファイルであるかどうかの確認は、次を使用して回避できます。

panel.canChooseFiles = false
于 2014-06-08T22:07:00.200 に答える
6
// 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
  }
}
于 2012-07-18T08:59:04.313 に答える