绑定到 Core Data 中的关系属性
我是核心数据的新手,我遇到了一个问题,我无法理解如何以“正确的方式”进行操作,
我将尝试举例说明我的问题。
我有一辆实体车。以及我的程序中所有汽车的列表。汽车有一些属性,但它们不是预定义的。因此,对于每辆车,我希望能够定义一些属性。 因此,我定义了一个新的实体 CarProperty,与汽车具有一对多的关系。
在 nscollectionview 中,我想显示汽车的一些属性,更具体的是它已经行驶的公里数(numKm)(如果该属性存在)。所以我想将它绑定到标签上。但怎么办呢?
我不能说 representedObject.properties.numKm
或 representedObject.numKm
。
我应该如何解决这个问题?
希望这是有道理的。
I'm new in Core Data, and i got a problem i can't get my head around how to do "the right way"
I'll try and examplify my problem.
I got a entity Car. And a list of all the cars in my program. The cars have some attributes, but they are not predefined. So for each car i want to be able to define some properties.
Therefore i have defined a new entity CarProperty, with a one to many relation with the car.
In the nscollectionview i would like to show some of the properties from the car, more specefic the number of kilometer (numKm) it has driven (if that property exist). So i want to bind it to a label. But how to do?
I can't say representedObject.properties.numKm
, or representedObject.numKm
.
How should I get around this?
Hope it makes sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是一个容易的问题。问题是,Core Data 对
numKm
作为属性一无所知。它如何知道numKm
对应于特定的CarProperty
对象?您描述的根本问题是键值编码合规性。 Cocoa 将在
properties
对象上查找名为numKm
的方法。如果找不到,它将尝试发送[properties valueForKey:@"numKm"];
因为valueForKey:
不知道如何处理numKm 时,您会收到错误,但在调用
[properties valueForUndefinedKey:@"numKm"]
之前不会出现错误,但问题是:
properties
是一个NSSet 由 Core Data 生成,因此您无法对其进行子类化以覆盖
valueForUndefinedKey:
。您可以做的是为您的任意属性创建符合 KVC 的您自己的对象并使用它。一种解决方案是子类化 NSDictionary 并使其充当代理。原始方法是
count
、objectForKey:
和keyEnumerator
。如果您重写这三个方法,则可以创建一个链接到您的Car
对象的NSDictionary
并返回相应的CarProperty
对象。例如:然后,在您的
Car
类中,执行以下操作:(免责声明:我只是将其输入到我的网络浏览器中,因此不能保证它实际上可以编译:-))
如您所见,这不是世界上最容易做的事情。您将能够像这样设置关键路径:
请记住,虽然这是符合键值编码的,但它不符合键值观察的。因此,如果
numKm
发生变化,您将无法观察到这一点。您需要做一些额外的工作才能实现这一点。This isn't an easy problem. The thing is, Core Data doesn't know anything about
numKm
as a property. How is it supposed to know thatnumKm
corresponds to a particularCarProperty
object?The fundamental problem you're describing is key-value coding compliance. Cocoa's going to look for a method called
numKm
on theproperties
object. Not finding one, it'll try sending[properties valueForKey:@"numKm"];
SincevalueForKey:
doesn't know what to do withnumKm
, you get an error, but not before it calls[properties valueForUndefinedKey:@"numKm"]
But here's the catch:
properties
is anNSSet
generated by Core Data, so you can't subclass it to overridevalueForUndefinedKey:
. What you can do is create your own object that's KVC-compliant for your arbitrary properties and use that instead.One solution is to subclass
NSDictionary
and make it act as a proxy. The primitive methods arecount
,objectForKey:
andkeyEnumerator
. If you override these three methods, you can create anNSDictionary
that's linked to yourCar
object and returns the appropriateCarProperty
objects. For example:Then, in your
Car
class, do this:(Disclaimer: I just typed this into my web browser, so no guarantees this actually compiles :-))
As you can see, it's not the easiest thing in the world to do. You'll be able to set up key paths like this:
Keep in mind that, while this is key-value coding compliant, it is not key-value observing compliant. So if
numKm
changes, you won't be able to observe that. You would need to do some extra work to make that happen.