【发布时间】:2014-01-08 15:16:35
【问题描述】:
试图确定 NSOpenPanel 是否返回了文件或目录
我已将 Apple 的示例代码用于 fileExistsAtPath:,它适用于 fontPath 但似乎不适用于openpanel 不确定从 NSURL 获取 NSString 是否正确 - 我仍然是 Cocoa 新手
它确实表明存在语义问题 将 'const BOOL *' (aka 'const signed char *') 发送到 'BOOL *' (aka '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,则可以在此处放置一个 else 条件,然后打印它是一个文件,否则在 else 条件下打印它是一个目录。
标签: cocoa