Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 您如何获得Finder;“种类”;为了一个文件?_Cocoa_File Type_Finder_Uti - Fatal编程技术网

Cocoa 您如何获得Finder;“种类”;为了一个文件?

Cocoa 您如何获得Finder;“种类”;为了一个文件?,cocoa,file-type,finder,uti,Cocoa,File Type,Finder,Uti,我想找到一个文件的“种类”。例如,对于文件“foo.css”,我需要字符串“css样式表” 到目前为止,我正在做这样的事情: NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: filename]; NSString *utiType; NSError *error; BOOL success = [fileURL getResourceValue: &utiType

我想找到一个文件的“种类”。例如,对于文件“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,因此显然与查找器在“种类”列中显示的内容不同


如何获取文件类型?

尝试
LSCopyKindStringForURL
,传递文件URL。

我能够获取.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