[英]Get file type description by extension
How can I get the description of the file type like it does in Finder.app by using only file extension? 如何仅使用文件扩展名来获取文件类型的描述,就像在Finder.app中一样? In other words I want to get that field in NSString:
换句话说,我想在NSString中获取该字段:
Here's a little something quick 'n' dirty that illustrates what you need: 以下是一些快速的“ n”表示您需要的东西:
NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
[openPanel runModal];
NSString *path = [[openPanel URL] path];
NSString *type = [[NSWorkspace sharedWorkspace] typeOfFile:path error:NULL];
NSLog(@"%@", [[NSWorkspace sharedWorkspace] localizedDescriptionForType:type]);
The open panel lets you choose a file. 打开面板可以选择一个文件。
NSWorkspace
provides for first determining the UTI of the file (given its path), and then using the UTI to get the localized string describing the file type. NSWorkspace
提供确定文件的UTI(给定其路径),然后使用UTI获取描述文件类型的本地化字符串。
EDIT: 编辑:
If you positively have got to use only the file extension, then use these three lines instead of the last three above: 如果肯定只需要使用文件扩展名,则使用以下三行而不是上面的最后三行:
NSString *extension = [[[openPanel URL] path] pathExtension];
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef) extension, NULL);
NSLog(@"%@", [[NSWorkspace sharedWorkspace] localizedDescriptionForType:(__bridge NSString *) uti]);
您应该通过将文件通用类型标识符提供给NSWorkspace的localizedDescriptionForType:
来实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.