Пытаясь выяснить, если NSOpenPanel вернулся файл или режПроверка NSOpenPanel возвращает каталог
Я использовать пример код от компании Apple для fileExistsAtPath: и она отлично работает для шрифтов , но, кажется, не работает для openpanel не уверен, если я делаю правильную вещь с получением NSString из NSURL - Я все еще какао новичку
Это указывает на семантическую вопрос Отправка «константный BOOL *» (он же «Const подписал полукокса * ') к параметру типа «BOOL *» (он же «подписанный символ *») отбрасывает квалификаторы
Любая помощь, пожалуйста,
- (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 всегда будет проверять наличие файла или нет. Таким образом, вы можете поставить условие else здесь, если fileExistsAtPath возвращает true, а затем распечатать его файл или иначе, в противном случае условие print - это каталог. –