views:

417

answers:

2

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.

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?

A: 

This question, http://stackoverflow.com/questions/1834125/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.

TechZen
+1  A: 

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

Seventoes