重命名OSX的Xcode项目后的NSKeyedUnarchiver错误 [英] NSKeyedUnarchiver error after renaming Xcode project for OSX

查看:1478
本文介绍了重命名OSX的Xcode项目后的NSKeyedUnarchiver错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在更改项目名称后遇到了问题(我认为)的命名空间。这是错误:

I am having issues with (I think) the namespace after changing the project name. Here is the error:


由于未捕获异常而终止应用程序'NSInvalidUnarchiveOperationException',reason:'* - [NSKeyedUnarchiver decodeObjectForKey:]:对象(Apple.Document)的键(NS.objects);该类可以在源代码或未链接的库中定义

有一个类似的问题在这里,但这是指iOS。对于Mac,只需更改显示包名称将不会更改显示在dock中图标下的名称。

There is a similar question addressed here, but this is referring to iOS. For Mac, simply changing the display bundle name will not change the name displayed under the icon in the dock.

将项目名称从Apple更改为Pear后,
当从CoreData获取文档对象时遇到问题:

After changing the project name from "Apple" to "Pear", I am having issues when getting Document objects from CoreData:

    // Document
    @objc(Document)
    class Document: NSManagedObject {
        @NSManaged var content: NSAttributedString
    }

文档的 content 包含 SpecialTextAttachment s。

    // SpecialTextAttachment
    class SpecialTextAttachment: NSTextAttachment {
        //
    }

打印文档的内容时,上面提到的错误。我如何将 Apple.SpecialTextAttachment 转换为 Pear.SpecialTextAttachment ?看起来我需要转换它之前,我通过数组 Document s,因为只是引用 Document 类导致崩溃。我也无法为此类创建另一个项目,如此处所示。

When printing the Document's content, it gives me the error addressed above. How would I go about converting Apple.SpecialTextAttachment to Pear.SpecialTextAttachment? It seems like I need to convert it before I iterate through the array of Documents, because just referring to the Document class causes a crash. I also cannot create another project simply for this class as suggested here.

    // in ViewController
    func getDocuments() {
        let fetchRequest = NSFetchRequest()
        fetchRequest.entity = NSEntityDescription.entityForName("Document", inManagedObjectContext: managedObjectContext!)
        fetchRequest.fetchBatchSize = 20
        let docs = (try? managedObjectContext!.executeFetchRequest(fetchRequest)) as! [Document]

    }


推荐答案

所以在 init()我的类,其中包含所有的方法来处理CoreData的数据,我放置了以下代码:

So in init() of my class which contains all the methods for handling data from CoreData, I placed the following code:

class CoreDataHelper {
    init() {
        NSKeyedUnarchiver.setClass(SpecialTextAttachment.self, forClassName: "Apple.SpecialTextAttachment")
    }
}

这会将任何 Apple.SpecialTextAttachment 转换为 Pear.SpecialTextAttachment 在任何即将到来的操作中, SpecialTextAttachment 可能会出现。

This will convert any Apple.SpecialTextAttachment to Pear.SpecialTextAttachment in any upcoming operations in which SpecialTextAttachment may come up.

这篇关于重命名OSX的Xcode项目后的NSKeyedUnarchiver错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆