CoreData not setting type as XML

荒凉一梦 提交于 2019-12-11 07:12:25

问题


I'm having a hard time debugging a problem with my data (CoreData, NSPersistentDocument).

I have a subclass of NSPersistentDocument. I am using NSManagedObject subclasses / standard Core Data models. I'm not doing anything special in NSPersistentDocument or with the NSManagedObject classes. I am merely creating an object (in NSPersistentDocument's subclass):

[NSEntityDescription insertNewObjectForEntityForName:@"ModelName" 
    inManagedObjectContext:[self managedObjectContext]];

When I attempt to save the document in my app, there is a drop-down for file formats. It includes Binary (default), SQLite, and XML. I save the file as XML. When I try to view it (using less, or even opening in Finder), I find that the file is stored as binary.

Is there something special I need to go to force it to XML?

My understanding based on the documentation from Apple is that in using an NSPersistentDocument subclass, I don't need to do the work of setting up the NSPersistentStore or NSPersistentStoreCoordinator. My understanding is all of this comes for free. Also from what I've read, XML is the default.


回答1:


The template that Xcode creates for a document based app with Core Data works perfectly for what you're describing. You may need to include a bit more info, but one thing to check would be that the document types in your Info.plist are correct. Below are the out of the box values. There's also a graphical editor in Xcode for this under the Info tab when you have your target selected in the project view.

Try creating a new project with Core Data and Document Based Application checked (Xcode 4.3) and check to see if that works fine. If it does, then something in your configuration has changed to make it binary instead of XML.

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeExtensions</key>
        <array>
            <string>binary</string>
        </array>
        <key>CFBundleTypeMIMETypes</key>
        <array>
            <string>application/octet-stream</string>
        </array>
        <key>CFBundleTypeName</key>
        <string>Binary</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSTypeIsPackage</key>
        <false/>
        <key>NSDocumentClass</key>
        <string>Document</string>
        <key>NSPersistentStoreTypeKey</key>
        <string>Binary</string>
    </dict>
    <dict>
        <key>CFBundleTypeExtensions</key>
        <array>
            <string>sqlite</string>
        </array>
        <key>CFBundleTypeMIMETypes</key>
        <array>
            <string>application/octet-stream</string>
        </array>
        <key>CFBundleTypeName</key>
        <string>SQLite</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSTypeIsPackage</key>
        <false/>
        <key>NSDocumentClass</key>
        <string>Document</string>
        <key>NSPersistentStoreTypeKey</key>
        <string>SQLite</string>
    </dict>
    <dict>
        <key>CFBundleTypeExtensions</key>
        <array>
            <string>xml</string>
        </array>
        <key>CFBundleTypeIconFile</key>
        <string></string>
        <key>CFBundleTypeMIMETypes</key>
        <array>
            <string>text/xml</string>
        </array>
        <key>CFBundleTypeName</key>
        <string>XML</string>
        <key>CFBundleTypeOSTypes</key>
        <array>
            <string>????</string>
        </array>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSTypeIsPackage</key>
        <false/>
        <key>NSDocumentClass</key>
        <string>Document</string>
        <key>NSPersistentStoreTypeKey</key>
        <string>XML</string>
    </dict>
</array>


来源:https://stackoverflow.com/questions/9404962/coredata-not-setting-type-as-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!