xcode Swift 3 核心数据“实体”错误:`未声明类型的使用`
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39934740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift 3 Core Data "Entity" error: `Use of undeclared type`
提问by PlateReverb
Use of undeclared type 'Transcription'
Use of undeclared type 'Transcription'
I'm following this simple tutorial of Core Data in Swift 3 (https://learnappdevelopment.com/uncategorized/how-to-use-core-data-in-ios-10-swift-3/)
我正在关注 Swift 3 中核心数据的这个简单教程(https://learnappdevelopment.com/uncategorized/how-to-use-core-data-in-ios-10-swift-3/)
and I get the above error on the line: let fetchRequest: NSFetchRequest<Transcription> = Transcription.fetchRequest()
我在线上收到上述错误: let fetchRequest: NSFetchRequest<Transcription> = Transcription.fetchRequest()
I double checked and the Entity "Transcription" is spelled correctly in my .xcdatamodeld file
我仔细检查了实体“转录”在我的 .xcdatamodeld 文件中拼写正确
The tutorial was designed for Swift 3, but there was another change since it was released that I fixed, so I'm guessing some other change to Swift in the past 2 months has caused this error.
本教程是为 Swift 3 设计的,但自发布以来我修复了另一个更改,因此我猜测过去 2 个月对 Swift 的其他更改导致了此错误。
I'm brand new to Core Data, so I don't know how to debug this. I'd be very grateful for a solution!
我是 Core Data 的新手,所以我不知道如何调试它。我将非常感谢解决方案!
回答by Badre
Dont forget import CoreData
to your subclass
不要忘记import CoreData
你的子类
回答by Marco Leong
This happened possibly as Xcode is unable to determine the pathof the .xcdatamodel
, a related error can be seen when cleaning the the project. This error happen to one of my project when changing the nameof the .xcdatamodel
.
这可能是因为 Xcode无法确定 的路径,因此.xcdatamodel
在清理项目时可以看到相关错误。当这个错误发生在我的一个项目更改名称的.xcdatamodel
。
Resolution involves the following:
解决涉及以下内容:
- go to Product -> Clean (Shift-Cmd-K)
- if the error persists
- Ctrl-click (right-click)the problematic
.xcdatamodel
-> Show in Finder - Dragthe
.xcdatamodel
to import to the project, a new entry should be created - Removethe previous
.xcdatamodel
entry.
- Ctrl-click (right-click)the problematic
- Build (Cmd-B)to confirm the fix
- 转到产品 -> 清洁(Shift-Cmd-K)
- 如果错误仍然存在
- 按住 Ctrl 键单击(右键单击)有问题的
.xcdatamodel
->在 Finder 中显示 - 拖动的
.xcdatamodel
导入到项目中,一个新的项应创建 - 删除上一个
.xcdatamodel
条目。
- 按住 Ctrl 键单击(右键单击)有问题的
- 构建(Cmd-B)以确认修复
回答by Leibniz
I was troubled the same error,I used to Xcode 8.2.1 . I cleaned the project ,closed the Xcode and reopen it ,the error was eliminated after building.
我被同样的错误困扰,我习惯于 Xcode 8.2.1 。我清理了项目,关闭了 Xcode 并重新打开它,构建后错误消除了。
回答by PlateReverb
Highlight the Data Model, go to Editor -> Create NSManagedObject Subclass...
突出显示数据模型,转到 Editor -> Create NSManagedObject Subclass...
This solved the error
这解决了错误
回答by Laura Corssac
In my case, Xcode
wasn't finding the automatically generated class, which is something like that:
就我而言,Xcode
没有找到自动生成的类,类似于:
//
// Teste+CoreDataClass.swift
//
//
// Created by Laura Corssac on 2/2/20.
//
// This file was automatically generated and should not be edited.
//
import Foundation
import CoreData
@objc(Teste)
public class Teste: NSManagedObject { }
After quitting Xcode
and reopening it, my problem was solved.
退出Xcode
并重新打开后,我的问题就解决了。
回答by Peter Gits
Using Xcode Version 8.2 beta (8C30a), when I add or modify the entity, I get the same error. In the data model inspector, change the Codegen option back to Manual/None, as if I was going to generate the dynamic classes myself. Save, then change back to Category/Extension, and it seemed to work. (I also had tried cleaning the project with Option-Clean, restarting XCode, but was able to reproduce it easily.) Hope the upgrade 8.3 beta fixes this bug.
使用 Xcode 8.2 测试版 (8C30a),当我添加或修改实体时,我得到同样的错误。在数据模型检查器中,将 Codegen 选项改回 Manual/None,就好像我要自己生成动态类一样。保存,然后改回类别/扩展,它似乎工作。(我也曾尝试使用 Option-Clean 清理项目,重新启动 XCode,但能够轻松重现它。)希望升级 8.3 测试版修复此错误。
回答by Дмитрий Толмачев
No. 1) We create subclass, go to Editor -> Create NSManagedObject Subclass... Then replace subclass files to the folder to the same project:
不。 1)我们创建子类,转到 Editor -> Create NSManagedObject Subclass... 然后将子类文件替换到同一个项目的文件夹中:
2) then delete files in derivedData - in Xcode -> Preferences:
2) 然后删除导出数据中的文件 - 在 Xcode -> 首选项中:
3) and then delete reference to old files of NSManagedObject Subclass, because they create automatically system:
3) 然后删除对 NSManagedObject 子类的旧文件的引用,因为它们会自动创建系统:
回答by Connor Joseph Early
For me, I had to delete my entity that was giving me the error, in your case 'Transcription', from my .xcdatamodeld file and then re-add it again by hand.
对我来说,我不得不从我的 .xcdatamodeld 文件中删除给我错误的实体,在你的情况下是“转录”,然后手动重新添加它。