如何在目标中查找文件类型 [英] How to find file types in objective c

查看:122
本文介绍了如何在目标中查找文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了大量的研究,没有发现任何有用的东西。我想制作一个简单的个人用途的程序,打开文件并提供关于它们的信息。我遇到的唯一的问题是,我无法找到我打开的文件的文件。没有简单的看扩展名,是否有一种方法来找到一个文件的完整文件格式在目标c?

如果可能的话,我也希望能够以不同的格式保存该文件。有关此主题的信息对于此应用程序也很重要。帮助,将不胜感激。
解决方案

Mac OS X具有附加到每个文件的类型信息,指定文件的类型是应该是。这个信息由上次保存文件的应用程序给出,所以不一定是正确的。此外,OS X的新版本忽略这些信息,完全脱离文件扩展名。但是,信息仍然存储,可以使用NSFileManager的 attributesOfItemAtPath:error:方法检索。如上所述,OS X现在将扩展映射到UTI。一个文件的UTI可以使用NSWorkspace来检索,这也可以告诉你UTI是什么意思。这段代码将得到 / full / path / to / file 文件的本地化描述:

  NSWorkspace * ws = [NSWorkspace sharedWorkspace]; 
NSString * description = [ws localizedDescriptionForType:[ws typeOfFile:@/ full / path / to / fileerror:nil]];

typeOfFile:error:绝对路径。


I have done a lot of research on this and haven't found anything useful. I want am making a simple program in objective c for personal use that opens files and gives information about them. The only problem i have encountered is that i cannot find the file of a file i am opening. Without simply looking at the extension, is there a way to find the full file format of a file in objective c?

If possible, i would also like to be able to save that file in a different format. Information on this subject is also important for this application. Help will be greatly appreciated.

解决方案

Mac OS X has type information attached to each file which specifies what the type of the file is supposed to be. This information is given by the application which last saved the file, so it is not necessarily correct. Also, new versions of OS X ignore this information and go entirely off of the file extension. However, the information is still stored and can be retrieved using NSFileManager's attributesOfItemAtPath:error: method.

As mentioned by quixoto above, OS X now maps extensions to UTIs. The UTI of a file can be retrieved using NSWorkspace, which can also tell you what the UTI means. This code will get the localized description of the file at /full/path/to/file:

NSWorkspace *ws = [NSWorkspace sharedWorkspace];
NSString *description = [ws localizedDescriptionForType:[ws typeOfFile:@"/full/path/to/file" error:nil]];

The typeOfFile:error: method requires an absolute path.

这篇关于如何在目标中查找文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆