问题
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key active.'
感谢@GrahamPerks answer对此SO question ,我在我的代码中插入了一个异常断点,现在在这一行暂停执行...
static var entityActive: Bool {
return entity.value(forKey: "active") as! Bool // <-- PAUSES AT THIS LINE
}
这显然需要进一步解释......
背景
我正在编写一个使用通用 TableView 数据源和委托(delegate)的 Core Data 应用程序,或多或少是根据 Florian Kugler 于 2017 年由 objc.io 出版的书“Core Data”设置的.
我已经成功地将三个单独的 UITableViewController
链接到这个通用数据源/委托(delegate)。我使用的是一个主 Storyboard,这三个 Controller 链接到三个 UISplitViewController
主视图/详细 View 。
我已经删除了 Storyboard UITableView
中自动生成的数据源和委托(delegate)连接(尽管我是否保留或删除这些连接似乎没有什么区别)。
如果我注释掉上面的 static var entityActive
代码,项目将成功构建并运行。
我的代码使用 UITableViewDelegate
方法 tableView(_, willDisplay:, forRowAt:)
来改变文本的 .textColor
和 .backgroundColor
单元格,基于属性“active”,存储为每个实体的 bool 标量类型值。
为清楚起见,我尝试为实体的每个 NSManagedObject
获取“active”属性(我的数据模型中的每个实体都有一个公共(public)属性“active”)。然后使用每个实体的“事件”属性(Bool
true
或 false
)的值来格式化 if.. .else 语句在下面的代码中。
为了尝试最大化代码重用和最小化代码重复,我还将此委托(delegate)方法放在我的通用数据源/委托(delegate)类中。
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let entityObjectActive = T.entityActive
if entityObjectActive == true {
cell.textLabel?.textColor = UIColor.black
cell.detailTextLabel?.textColor = UIColor.darkGray
cell.backgroundColor = UIColor.white
} else if entityObjectActive == false {
cell.textLabel?.textColor = UIColor.lightGray
cell.detailTextLabel?.textColor = UIColor.lightGray
cell.backgroundColor = UIColor.clear
}
}
编译器不会提示。
我似乎能够使用通用类型 T
(代表核心数据实体)将静态属性 entityActive
与我的 entityObjectActive实例相关联
- 所以这似乎有效...
let entityObjectActive = T.entityActive
为了确认,我有以下内容:
通用数据源类....
class MyDataSource<T: Managed,
Delegate: TableViewDataSourceDelegate>:
NSObject,
UITableViewDataSource,
UITableViewDelegate,
NSFetchedResultsControllerDelegate {
// lots of code...
}
协议(protocol)...
protocol Managed: class, NSFetchRequestResult {
static var entity: NSEntityDescription { get }
static var entityActive: Bool { get } }
}
和扩展...
extension Managed where Self: NSManagedObject {
static var entity: NSEntityDescription { return entity() }
static var entityActive: Bool {
return entity.value(forKey: "active") as! Bool
}
}
尝试解决问题
我读了一些书试图解决我的问题,包括对许多博客的回顾(关于如何设置通用泛型和通用 TableView 数据源)和大量 SO 问答,特别是...
How to fix Error: this class is not key value coding-compliant for the key tableView.'
Uncaught exception: This class is not key value coding-compliant
setValue:forUndefinedKey: this class is not key value coding-compliant for the key
似乎所有 SO Q&A 都与 Storyboard中的 IB 连接问题直接相关。也许这也是我的问题 - 但如果是这样的话,在我看来它隐藏得很好。
有什么帮助或建议吗?
PS:我在长期使用 Obj-C 编写代码后正在学习 Swift,我真的在努力应对泛型协议(protocol)扩展范式转变,所以我对泛型类型的使用可能不正确?
最佳答案
感谢那些将我引向这个解决方案的评论...
我的修订协议(protocol) Managed
...
protocol Managed: class, NSFetchRequestResult {
static var entity: NSEntityDescription { get }
var attributeActive: Bool { get } // <-- REMOVED static
}
我对 Managed
的修订扩展...
更新 - 添加 var attributeActive
到 Managed
扩展...
extension Managed where Self: NSManagedObject {
static var entity: NSEntityDescription { return entity() }
var attributeActive: Bool {
guard let attribute = self.value(forKey: "active") as? Bool else {
return false // in case key "active" is not set
}
return attribute
}
}
更新 - 从不再需要的托管对象扩展中删除 var attributeActive
...
我的三个核心数据实体中的每一个的修订扩展(其数据显示在三个单独的 UITableViewController
中的每一个中)...
extension <<DataModelEntity>>: Managed {
public var attributeActive: Bool {
return self.active
}
@NSManaged public var active: Bool
@NSManaged public var <<OTHER DATA MODEL ENTITY ATTRIBUTES>> //...
// ...etc.
}
为了清楚起见,这里可能值得注意的是,数据模型中每个实体的 Codegen 值都设置为 Manual/None,所以我手动 1 为每个实体准备类和扩展。
1 当我手动写的时候,我的意思是我使用了Editor下的Create Managed Object Subclass...功能 Xcode 中的菜单,然后手动输入我的 Managed
协议(protocol) stub 。
最后,我的 REVISED 通用数据源委托(delegate)类...
class MyDataSource<T: Managed,
Delegate: TableViewDataSourceDelegate>:
NSObject,
UITableViewDataSource,
UITableViewDelegate,
NSFetchedResultsControllerDelegate {
// lots of code...
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let object = fetchedResultsController.object(at: indexPath)
let objectActive = object.attributeActive
if objectActive == true {
cell.textLabel?.textColor = UIColor.black
cell.detailTextLabel?.textColor = UIColor.darkGray
cell.backgroundColor = UIColor.white
} else if entityObjectActive == false {
cell.textLabel?.textColor = UIColor.lightGray
cell.detailTextLabel?.textColor = UIColor.lightGray
cell.backgroundColor = UIColor.clear
}
}
// lots more code...
}
如果有人仍在阅读这篇文章,也许您对这个解决方案的原因感兴趣?
坦率地说,我自己仍在弄清楚细节,并计划在未来添加更简洁/准确的原因,因为我对 swift 泛型、协议(protocol)和扩展的理解有所提高,但现在我提供以下内容......
正如评论中所指出的,我错误地尝试根据实体描述中的数据模型属性获取实体属性。这就像通过向乐高盒子询问其中一 block 积木的特性来尝试获得乐高积木的颜色或大小。 “哪 block 砖?”盒子可能会问,如果乐高盒子可以问这样的事情。
基本上我犯了几个错误。我不明白静态定义对我的变量的影响,我也没有正确理解我的 Managed
协议(protocol)和扩展如何与采用该协议(protocol)的任何类交互。
关于ios - Swift 通用 TableView 数据源和委托(delegate),协议(protocol)支持 NSManagedObjects,无法获取实体属性,运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53271766/