Getting extensions of a given UTType
Swift 5
import MobileCoreServices
static func fileExtension(for dataUTI: String) -> String? {
guard let fileExtension = UTTypeCopyPreferredTagWithClass(dataUTI as CFString, kUTTagClassFilenameExtension) else {
return nil
}
return String(fileExtension.takeRetainedValue())
}
UTTypeCopyPreferredTagWithClass
is used to convert a UTI to another tag, like file extensions:
NSString *extension = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(myUTI, kUTTagClassFilenameExtension);