如何使用 Swift 创建具有多个扩展的自定义 UTType?

问题描述 投票:0回答:1

我想创建一个自定义

UTType
来支持多个扩展(cxf和rwxf)

类似于

UTType.tiff

UTType.tiff.tags
[
    public.filename-extension: ["tiff", "tif"], 
    public.mime-type: ["image/tiff"], 
    com.apple.nspboard-type: ["NeXT TIFF v4.0 pasteboard type", "NSTIFFPboardType"], 
    com.apple.ostype: ["TIFF"]
]

目前!我可以为自定义 UTType 创建 1 个文件扩展名。
如何创建具有多个扩展名(cxf 和 rwxf)的自定义 UTType?

UTType(tag: "cxf", tagClass: .filenameExtension, conformingTo: .data)
swift uttype
1个回答
0
投票

您可以在项目设置的“导出/导入类型标识符”部分添加任意数量的文件扩展名。

这是一个示例,其中我添加了“abc”和“def”作为导出类型标识符:

enter image description here

对应Info.plist代码:

<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.data</string>
        </array>
        <key>UTTypeDescription</key>
        <string>Some Description</string>
        <key>UTTypeIconFiles</key>
        <array>
            <string>icon</string>
        </array>
        <key>UTTypeIdentifier</key>
        <string>com.example.example</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>abc</string>
                <string>def</string>
            </array>
            <key>public.mime-type</key>
            <array/>
        </dict>
    </dict>
</array>

然后您可以在代码中将其作为

UTType
获取,如下所示:

print(UTType(exportedAs: "com.example.example").tags)
// prints '[public.filename-extension: ["abc", "def"]]'
© www.soinside.com 2019 - 2024. All rights reserved.