4
我一直在嘗試轉換這個代碼,我從這個example(在Objective-c中)沒有運氣。如何找到文件UTI文件,沒有pathExtension,在Swift中的路徑
String *path; // contains the file path
// Get the UTI from the file's extension:
CFStringRef pathExtension = (__bridge_retained CFStringRef)[path pathExtension];
CFStringRef type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, NULL);
CFRelease(pathExtension);
// The UTI can be converted to a mime type:
NSString *mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType);
if (type != NULL)
CFRelease(type);
我的代碼是這樣的
import MobileCoreServices
let path: String?
let pathExt = path.pathExtension as CFStringRef
let type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, NULL);
let mimeType = UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType)
if type != Null
CFRelease(type)
所有我想要做的就是找出一個文件是
如果你看一下你試圖移植的代碼以及你正在移植它的答案的評論,你會注意到它沒有做你所需要的 - 它從文件名獲得擴展名,然後查找MIME類型爲此擴展。如果你想要的類型信息沒有擴展名,你需要查看文件的前幾個字節[magic numbers](http://en.wikipedia.org/wiki/Magic_number_(編程))。 iOS不包括一個API,但[有第三方選項](http://stackoverflow.com/q/3173267/957768)。 – rickster 2015-02-17 21:21:59