2014-01-08 66 views
0

試圖確定是否NSOpenPanel返回一個文件或目錄檢查是否NSOpenPanel返回目錄

我已經使用來自蘋果的示例代碼fileExistsAtPath:它工作正常的字體路徑 但似乎並沒有工作對於openpanel 不知道我在做正確的事與NSURL得到的NSString - 我還是一個新手可可

它確實表明語義問題 發送「常量BOOL *」(又名「常量符號字符*')到類型'BOOL *'(又名'signed char *')的參數丟棄限定符

任何幫助,請

- (IBAction)openImage: (id)sender 
{ 
    // present open panel... 

NSString * extensions = @"tiff/tif/TIFF/TIF/jpg/jpeg/JPG/JPEG/CR2"; 
NSArray *  types = [extensions pathComponents]; 
NSFileManager *fileManager = [[NSFileManager alloc] init]; 

//=================================== 
// example just to see if it works!! 
NSArray *subpaths; 
BOOL isDir; 
NSArray *paths = NSSearchPathForDirectoriesInDomains 
(NSLibraryDirectory, NSUserDomainMask, YES); 
if ([paths count] == 1) { 
    NSString *fontPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Fonts"]; 
    if ([fileManager fileExistsAtPath:fontPath isDirectory:&isDir] && isDir) { 
     NSLog(@"======= fontPath = %@", fontPath);   
    } 
} 
//============================================ 

// Let the user choose an output file, then start the process of writing samples 
NSOpenPanel *openPanel = [NSOpenPanel openPanel]; 
[openPanel setAllowedFileTypes:types]; 
[openPanel setCanSelectHiddenExtension:YES]; 
[openPanel setCanChooseDirectories:YES]; 
[openPanel beginSheetModalForWindow:_window completionHandler:^(NSInteger result) { 
    if (result == NSFileHandlingPanelOKButton) 
    { 
      // user did select an image... 

     NSLog(@"URL = %@",[openPanel URL]); 
     NSString *workFile = [[openPanel URL] absoluteString]; 
     NSLog(@"workFile %@",workFile); 
     if ([fileManager fileExistsAtPath:workFile isDirectory:&isDir] && isDir) { 
      NSLog(@"======== It's a dir======="); 
     } 


     [self openImageURL: [openPanel URL]]; 
     } 
    }]; 
} 
+0

fileExistsAtPath將始終檢查文件是否存在。所以,如果fileExistsAtPath返回true,那麼你可以在這裏放一個條件,然後打印它是一個文件或其他內部條件打印它是一個目錄。 –

回答

1

當塊引用塊本身然後之外聲明的局部(堆棧)變量:

  • 一個恆定的相同類型和名稱的本地變量的被添加到塊;和

  • 當前值局部變量的作爲價值塊的不斷

這就是爲什麼你得到一個錯誤引用const BOOL,你正試圖通過地址該塊的常量isDir其中預期爲非常量的地址。

可以傳遞isDir到塊作爲可變,使用在其聲明,這意味着塊內的isDir用途指__block限定符完全相同的可變作爲一個塊之外聲明。

但是從您的意見看來,這似乎並不是您所需要的,而是您只需要一個塊的變量用於方法調用和if語句。爲此只需在塊中聲明一個變量。您已經在該塊內聲明瞭NSString *workFile,只需以同樣的方式聲明本地布爾值即可。

HTH

0

The __block Storage Type。您正在傳遞對isDirfileExistsAtPath:isDirectory:的引用,該引用正在修改isDir變量,並且isDir變量由作爲completionHandler傳遞的塊複製。

The __block Storage Type

可以指定一個導入的變量是可變的,即, 通過應用__block存儲類型改性劑讀 - 直寫。

+0

謝謝,我可以看到問題在於存在塊。已經閱讀了一些關於塊的文檔,不能說我完全理解這個概念 - 這是我第一次真正嘗試Cocoa:} 我試過聲明 __block BOOL isDir; 在各地,無濟於事。 我該如何做到這一點? 我只想顯示一個NSOpenPanel 如果用戶選擇一個文件,然後打開它。 如果他們選擇了一個目錄,然後做一些其他的事情。 Ta – user1321964

0

想我現在明白了,謝謝大家。還認爲fileExistsAtPath需要一個文件路徑,因此URL必須使用myUrl進行轉換。路徑

NSOpenPanel *openPanel = [NSOpenPanel openPanel]; 
[openPanel setAllowedFileTypes:types]; 
[openPanel setCanSelectHiddenExtension:YES]; 
[openPanel setCanChooseDirectories:YES]; 
[openPanel beginSheetModalForWindow:_window completionHandler:^(NSInteger result) { 
    if (result == NSFileHandlingPanelOKButton) 
    { 
      // user did select an image... 
      // get list of files in this dir 

     NSURL *myUrl = [openPanel URL]; 
     NSString *workFile = myUrl.path; 
     NSLog(@"workFile %@",workFile); 
     BOOL isDir; 
     if ([fileManager fileExistsAtPath:workFile isDirectory:&isDir] && isDir) { 
      NSLog(@"---------It's a dir---"); 

     } 

    } 
}];