这就是我要求的测试方法..
NSString *fileType = [sharedWorkspace typeOfFile: [[NSUserDefaults standardUserDefaults] stringForKey: @"externalapp"] error:nil];
if (( UTTypeEqual((CFStringRef)fileType, kUTTypeApplication) ) || ( UTTypeEqual((CFStringRef)fileType, kUTTypeApplicationBundle) ) || ( UTTypeEqual((CFStringRef)fileType, kUTTypeApplicationFile) ))
NSLog(@"Yes - File type is equal to Application");
这是一个正确的变压器实现:
@implementation icalValidTransformer
+ (Class)transformedValueClass { return [NSNumber class]; }
+ (BOOL)allowsReverseTransformation { return NO; }
- (id)transformedValue:(id)value {
if (( value == nil ) || ( [[NSApp delegate]sharedWorkspace] == nil ))
return [NSNumber numberWithBool: NO];
NSError *fileError = nil;
NSString *fileType = [[[NSApp delegate] sharedWorkspace] typeOfFile:value error: &fileError];
if ( fileError != nil )
return [NSNumber numberWithBool: NO];
if (( UTTypeEqual((CFStringRef)fileType, kUTTypeApplication) ) || ( UTTypeEqual((CFStringRef)fileType, kUTTypeApplicationBundle) ) || ( UTTypeEqual((CFStringRef)fileType, kUTTypeApplicationFile) ))
return [NSNumber numberWithBool: YES];
return [NSNumber numberWithBool: NO];
}
@end
这也是一个非常好的示例,说明如何将 fileError 与 -typeOfFile 一起使用 - 如果出现错误(例如,文件不存在),此禁用的按钮/菜单/您正在使用的任何控件..