You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xcode文档应用模板开发Mac文本编辑器:自定义保存格式与保存方式

实现Mac版Document App自定义保存格式功能

1. 配置支持的文档类型(UTType)

首先需要让你的App同时支持多种可读写的文档类型,包括原有的.exampletext和.txt,后续也可扩展其他格式。

1.1 声明自定义UTType(针对.exampletext)

如果.exampletext是你自定义的格式,需要先在项目中注册这个类型。可以通过代码扩展UTType,或者在Info.plist中配置:

import UniformTypeIdentifiers

extension UTType {
    static var exampleText: UTType {
        UTType(exportedAs: "com.yourcompany.exampletext")
    }
}

注意:com.yourcompany.exampletext是反向域名格式的唯一标识符,替换成你自己的域名或项目标识。

1.2 修改Document类的可读写类型

在你的Document.swift中,同时修改readableContentTypes和writableContentTypes,包含所有支持的格式:

class Document: UIDocument {
    var text = ""

    // 支持读取的格式
    static var readableContentTypes: [UTType] {
        [.txt, .exampleText]
    }

    // 支持写入的格式
    static var writableContentTypes: [UTType] {
        [.txt, .exampleText]
    }

    // 其他代码...
}

2. 处理不同格式的读写逻辑

需要在contents(forType:)和load(fromContents:ofType:)方法中,针对不同格式做对应的处理:

override func contents(forType typeName: String) throws -> Any {
    guard let utType = UTType(typeName) else {
        throw NSError(domain: "DocumentError", code: -1, userInfo: [NSLocalizedDescriptionKey: "不支持的文件格式"])
    }

    // 纯文本格式(.txt)
    if utType == .txt {
        return text.data(using: .utf8)!
    }
    // 自定义格式(.exampletext),可添加额外标识或格式
    else if utType == .exampleText {
        let formattedText = "// ExampleText v1.0\n\(text)"
        return formattedText.data(using: .utf8)!
    }
    else {
        throw NSError(domain: "DocumentError", code: -1, userInfo: [NSLocalizedDescriptionKey: "不支持的文件格式"])
    }
}

override func load(fromContents contents: Any, ofType typeName: String?) throws {
    guard let data = contents as? Data else { return }
    var loadedText = String(data: data, encoding: .utf8) ?? ""

    // 处理自定义格式的内容(移除标识头)
    if let typeName = typeName, UTType(typeName) == .exampleText {
        loadedText = loadedText.replacingOccurrences(of: "// ExampleText v1.0\n", with: "")
    }

    text = loadedText
}

3. 配置Info.plist文档类型

Xcode的Document App依赖Info.plist的配置来识别文档类型,需要添加对应的条目:

  1. 右键项目中的Info.plist,选择Open As -> Source Code
  2. 添加以下XML代码(替换成你自己的类型标识):
<!-- 注册自定义UTType -->
<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeIdentifier</key>
        <string>com.yourcompany.exampletext</string>
        <key>UTTypeDescription</key>
        <string>Example Text Document</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>exampletext</string>
            </array>
        </dict>
    </dict>
</array>

<!-- 配置App支持的文档类型 -->
<key>CFBundleDocumentTypes</key>
<array>
    <!-- 纯文本格式 -->
    <dict>
        <key>CFBundleTypeName</key>
        <string>Text Document</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>public.plain-text</string>
        </array>
    </dict>
    <!-- 自定义格式 -->
    <dict>
        <key>CFBundleTypeName</key>
        <string>Example Text Document</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.yourcompany.exampletext</string>
        </array>
    </dict>
</array>

4. 测试保存格式选择

运行App后,当你保存文档时:

  • 在保存对话框中点击格式下拉菜单,就能看到Text Document(.txt)和Example Text Document(.exampletext)两个选项
  • 选择不同格式,App会自动调用对应格式的读写逻辑处理内容

内容的提问来源于stack exchange,提问作者ge0graphaly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 10:35:19