本文介绍了获取Cocoa中的文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我成功地找到了一个指定文件的扩展文件类型(JPEG图像,TIFF图像,...),但是我正在寻找一些比较通用的东西,可以将图像,moovies,文本文件... 有没有办法在可可(或目标c)中实现这一点? 感谢您的帮助,解决方案您可以使用统一类型标识符。由于UTI被组织成层次结构,一种可能性是检查给定文件的优选UTI是否符合顶级UTI,例如。 public.image 为影片或 public.movie 为电影。核心服务框架包括对UTI运行的函数以及表示已知UTI的常量。 例如,处理文件扩展名: NSString * file = @...; //某些文件的路径 CFStringRef fileExtension =(CFStringRef)[file pathExtension]; CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,fileExtension,NULL); if(UTTypeConformsTo(fileUTI,kUTTypeImage))NSLog(@It's a image); else if(UTTypeConformsTo(fileUTI,kUTTypeMovie))NSLog(@这是一部电影); else if(UTTypeConformsTo(fileUTI,kUTTypeText))NSLog(@It's text); CFRelease(fileUTI); 如果您有MIME类型而不是文件扩展名,则可以使用 kUTTagClassMIMEType 而不是 kUTTagClassFilenameExtension 。 有关已知UTI的列表,请参阅本文档。 p> I succeeded to find the extended file type of a specified file ( JPEG image, TIFF Image, ...) but I am looking for something more generic that can categorize files in "big cathegories" like images, moovies, text files, ...It there a way to acheive this in cocoa (or objective-c) ?Thanks for your help, 解决方案 You could use Uniform Type Identifiers. Since UTIs are organised into hierarchies, one possibility is to check whether the preferred UTI for a given file conforms to a top-level UTI, e.g. public.image for images or public.movie for movies. The Core Services framework includes functions that operate on UTIs as well as constants representing known UTIs.For instance, working on file name extensions:NSString *file = @"…"; // path to some fileCFStringRef fileExtension = (CFStringRef) [file pathExtension];CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);if (UTTypeConformsTo(fileUTI, kUTTypeImage)) NSLog(@"It's an image");else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) NSLog(@"It's a movie");else if (UTTypeConformsTo(fileUTI, kUTTypeText)) NSLog(@"It's text");CFRelease(fileUTI);If you have a MIME type instead of a file name extension, you can use kUTTagClassMIMEType instead of kUTTagClassFilenameExtension.For a list of known UTIs, see this document. 这篇关于获取Cocoa中的文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!