1

我想为一个文件获取 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 不同显示在“种类”列中。

你如何获得文件的种类?

4

2 回答 2

2

尝试LSCopyKindStringForURL,传递文件 URL。

于 2013-12-10T21:48:37.187 回答
1

我能够使用typeOfFile:error:后跟localizedDescriptionForType:获取 .css 文件类型的字符串,但该字符串不是您想要的“CSS 样式表”,而是“Dashcode CSS 文档”。其他扩展的一些其他示例:

ext      typeOfFile                    localizedDescriptionForType
------------------------------------------------------------------
.css     com.apple.dashcode.css        Dashcode CSS Document
.php     public.php-script             PHP script
.html    public.html                   HTML text
.txt     public.plain-text             text
于 2013-12-10T22:15:43.120 回答