2012-07-18 18 views
9

Kullanıcının bir dosyayı kaydetmek için bir dizin seçmesine izin vermek istiyorum. ama url bir dosya değil bir dizin olduğundan emin olmak için nasıl? Gelecekte bu okuma herkes içinNSOpenPanel bir dizin seçin (bir dosya değil)

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

cevap

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

Güncelleme: aldı yolu ise

Swift, bir dosya Amaç için çalışır

panel.canChooseFiles = false 
+0

teknik kullanılarak önlenebilir Kontrolden -C de, 'false' yerine 'NO' kullanmama rağmen. –

+0

Evet, bu doğru ama Swift'de yanlış kullanmalısınız. –

İlgili konular