【问题标题】:Core Data commands crash ViewController even after delete即使在删除之后,Core Data 命令也会使 ViewController 崩溃
【发布时间】:2016-07-24 23:48:24
【问题描述】:

我试图将自动保存添加到我的基于 Core Data 的应用程序中,并且我在 CollectionViewItem 的 textDidChange 中添加了一行代码:

    theNote?.updateChangeCount(NSDocumentChangeType.ChangeDone)

这给了我很多错误,所以我注释掉了这一行。然后我删除了 CocoaAppCD.storedata 持久存储文件以使其更干净(我仍处于早期开发阶段,所以我持久存储中的所有内容都是随机测试材料)。

现在,我发现,Core Data 命令会使我的 ViewController 崩溃。具体是这个功能:

func createNewNotebook(folderURL: NSURL)
{

    let currentNotebook = Notebook(entity: sv.noteType, insertIntoManagedObjectContext:sv.context)

    currentNotebook.folderURLstring = folderURL.absoluteString

    let noteSet = currentNotebook.mutableSetValueForKey("contains")
    print(String(noteSet))

    intakeFilesFromFolder(noteSet, currentFolderURL: folderURL)

}

(Notebook 和 NoteEntity 是我的 Core Data 模型中的两种实体类型。)当 currentNotebook.folder... 或 let noteSet... 命令运行时,我在控制台中收到以下错误:

Brouillon.NoteEntity folderURLstring]: unrecognized selector sent to instance 0x6080000a3a20

2016-07-24 19:27:02.622 Brouillon[8006:361665] Failed to set (contentViewController) user defined inspected property on (NSWindow): -[Brouillon.NoteEntity folderURLstring]: unrecognized selector sent to instance 0x6080000a3a20

并且 WindowController 是空的,没有视图填充它(即使这些相同的语句在我添加 now-commented-out 行之前已经起作用)。但是,如果我绕过这个函数,Core Data 语句就不会运行,视图就会加载。我会认为如果我在数据库中留下任何脏东西,删除 CocoaAppCD.storedata 文件应该已经修复它(并且我在每次运行后继续删除该文件)。但似乎 Core Data 中的某些东西对我来说仍然很糟糕 - 有什么想法吗?

【问题讨论】:

    标签: swift macos cocoa core-data nscollectionview


    【解决方案1】:

    我想通了。问题是我声明了错误的实体类型:我的 sv.noteType 应该是 sv.notebookType,因为它是笔记本而不是 NoteEntity。 (当我创建实例化为 sv 的类时,会引入该错误,作为保存我的其他重复代码的类。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 2021-09-22
      • 1970-01-01
      相关资源
      最近更新 更多