試圖確定是否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]];
}
}];
}
fileExistsAtPath將始終檢查文件是否存在。所以,如果fileExistsAtPath返回true,那麼你可以在這裏放一個條件,然後打印它是一個文件或其他內部條件打印它是一個目錄。 –