将 AVFileType 转换为 UTType

Convert AVFileType to UTType

我正在尝试查询设备上 AVFoundation 支持的文件类型,然后将这些文件类型传递给 UIDocumentPickerViewController 支持的内容类型。 AVFoundation 将类型报告为 AVFileType,而选择器采用 UTTypeAVFileType 对我来说似乎相当不透明,也就是说我无法弄清楚这个 documentation means and how it reconciles with what is shown when I print them. Because they print out 看起来像带有原始值的枚举:

__C.AVFileType(_rawValue: dyn.ah62d46dzqm0gw23sqf40k4pts3y1g7pbru00g55ssvw067b4ge8046p0), __C.AVFileType(_rawValue: public.pls-playlist), __C.AVFileType(_rawValue: dyn.ah62d46dzqm0gw23sqf40k4pts3y1g7pbru00g55ssvw067b4gq80c7perf1w85puqzx1n6xq), __C.AVFileType(_rawValue: public.aifc-audio),

但是文档说 AVFileType 只是一个带有一些静态常量的结构。这个类型到底是什么,为什么它显示 rawValues,有什么方法可以提取该信息并将其用作选择器的文件类型?

您可以使用 AVFileType rawValue 简单地初始化一个新的 UTType 对象:


let utType = UTType(AVFileType.ac3.rawValue)  // <UTType 0x600001b68360> public.ac3-audio

extension AVFileType {
    var utType: UTType { UTType(rawValue)! }
}

AVFileType.ac3.utType  // <UTType 0x600001b68360> public.ac3-audio