NSDocument应用程序和文件扩展名

时间:2012-08-23 11:23:04

标签: objective-c cocoa nsdocument

我创建了一个基于文档的应用程序,需要打开具有特定扩展名的xml文本文件。当我使用NSDocument模板在Xcode中创建项目时,我指定了我想要的扩展名,一切正常。

在关于如何构建基于文档的应用程序的Apple指南之后,我去编辑Info.plist文件以添加之前缺少的UTI的详细信息。突然,我的应用程序停止打开我想要的扩展名的文件,实际上停止打开任何文件。此外,当我尝试保存文件时,保存对话框不再建议任何扩展名。

当我保存文件并从终端运行命令mdls时,我得到了

kMDItemContentTypeTree         = (
    "public.data",
    "public.item"
)

而不是public.xml我在Info.plist中设置了UTI。 似乎我的应用程序停止识别存储在Info.plist中的信息。有没有什么我必须在Xcode中连接才能使这个工作?

以下是我的Info.plist文件的相关部分:

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeExtensions</key>
        <array>
            <string>xmds</string>
        </array>
        <key>CFBundleTypeIconFile</key>
        <string>AppIcon</string>
        <key>CFBundleTypeName</key>
        <string>XMDS Script</string>
        <key>CFBundleTypeOSTypes</key>
        <array>
            <string>xmds</string>
        </array>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.dedalus.degs</string>
        </array>
        <key>NSDocumentClass</key>
        <string>Document</string>
    </dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.xml</string>
        </array>
        <key>UTTypeDescription</key>
        <string>XMDS Script</string>
        <key>UTTypeIconFile</key>
        <string>AppIcon</string>
        <key>UTTypeIdentifier</key>
        <string>com.dedalus.degs</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>com.apple.ostype</key>
            <array>
                <string>xmds</string>
            </array>
            <key>public.filename-extension</key>
            <array>
                <string>xmds</string>
            </array>
        </dict>
    </dict>
</array>

1 个答案:

答案 0 :(得分:3)

我使用Project的Target Info设置来设置Document Type和UTI,而不是Xcode 4.2和4.4中的Info.plist文件。这从Info.plist文件中提取并在您更改后修改它。请尝试使用它。

文档类型定义将处理扩展和扩展的类。导出的UTI代表您的应用具有权威性的扩展。请参阅此Stackoverflow问题: What are “Imported UTIs” in Xcode 4?

My Exported UTI符合public.data(“任何类型的简单字节流的基本类型,包括文件和内存数据”)。如果使用标识符,请确保文档类型和导出的UTI中的标识符相同。