2012-07-18 9 views
9

मैं उपयोगकर्ता को फ़ाइल सहेजने के लिए निर्देशिका चुनने देना चाहता हूं। लेकिन यह सुनिश्चित करने के लिए कि यूआरएल एक निर्देशिका नहीं है?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

तकनीकी तौर पर है कि उद्देश्य के लिए काम करता है का उपयोग करके बचा जा सकता है -सी भी, हालांकि मैं 'झूठी' के बजाय 'NO' का उपयोग करूंगा। –

+0

हां, यह सच है, लेकिन स्विफ्ट में आपको झूठी उपयोग करना चाहिए। –

संबंधित मुद्दे