如何在目标中查找文件类型 [英] How to find file types in objective 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]];
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屋!