2012-07-18 7 views
9

파일 저장을위한 디렉토리를 사용자가 선택하게하고 싶습니다. 그러나 URL이 파일이 아닌 디렉토리인지 확인하는 방법은 무엇입니까? 미래에이 글을 읽는 사람을위한NSOpenPanel 디렉토리가 아닌 파일을 선택하십시오.

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 
     } 
    } 
}]; 

답변

7
// 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 
    } 
} 
15

업데이트 : 촬상 된 경로 인 경우

는 스위프트에서 파일이 목적을 위해 작동

panel.canChooseFiles = false 
+0

기술적를 사용하여 피할 수 확인 -C도,'false' 대신에'NO'를 사용합니다. –

+0

예, 그렇습니다. Swift에서는 false를 사용해야합니다. –

관련 문제