我想为一个文件获取 Finder “Kind”。例如,对于文件“foo.css”,我想要字符串“CSS 样式表”。
到目前为止,我正在做这样的事情:
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: filename];
NSString *utiType;
NSError *error;
BOOL success = [fileURL getResourceValue: &utiType
forKey: NSURLTypeIdentifierKey
error: &error];
if (!success) {
NSLog(@"failure during reading utiType: error = %@", error);
return;
}
NSString *utiDescription = (__bridge NSString *) UTTypeCopyDescription((__bridge CFStringRef) utiType);
if (utiDescription) {
// use utiDescription here...
}
这为我提供了 UTI 的人类可读名称,在某些情况下效果很好(“foo.html”给出“HTML 文本”),但对于 CSS 文件,它返回 nil,所以它显然与 Finder 不同显示在“种类”列中。
你如何获得文件的种类?