开发者

Core Data and Runtime Key Value Coding

开发者 https://www.devze.com 2022-12-18 08:04 出处:网络
I was under the impression that with key-value coding, I could set any undefined attribute on a NSManagedObject at runtime and no exception would be thrown, but it wouldbe a way to hold objects attach

I was under the impression that with key-value coding, I could set any undefined attribute on a NSManagedObject at runtime and no exception would be thrown, but it wouldbe a way to hold objects attached to the model that are not in the data model.

For example, I have a Foo object that does not have a "bar" attribute. I though that at runtime, I could set it like so:

Foo *foo = [NSEntityDescription insertNewObjectForEntityForName:@"Foo" inManagedObjectContext:ManagedObjectContext];
[foo setValue:@"foobar" forUndefinedKey:@"bar"];

I would then expect the ManagedObjectContext to hold this value until I queried it later, but a save on the Managed Object Context would not store the bar value.

开发者_Python百科

The famous error keeps popping up when I run this code:

the entity Foo is not key value coding-compliant for the key "bar"

..so my question is: What am I not getting or doing wrong?


This question, NSManagedObject subclasses and setValuesForKeysWithDictionary:, might be relevant.

Conceptually, I don't think you can assign random values and keys safely. Doing so would seem likely to disrupt the object graph.


In your data model, you can set attributes as "Transient"

This allows you to set that attribute but it won't be saved in the store, but will still be available for querying and undo.

http://2pi.dk/tech/cocoa/transient_properties.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号