开发者_StackOverflowI have just redesigned my core data file to make use of relationships and am having trouble getting by head around how i do things with this new model.
I have an entity called data store that had a one-to-one relationship with a second entity called test (test has a many to one relationship with datastore). I am trying to figure out how to store what record in the test entity has been selected. From what i have found from my goggling i should write
[test addDrivingConditionsObject:datastore]
Is this how i should be doing it or is there a way to say
[datastore addTestObject:test]
which i would prefer.
Thanks
Assuming that there's a to-many relationship called drivingConditions
from test
to datastore
, then -addDrivingConditionsObject:
is the correct accessor name. Accessors need to be named based on the relationship name rather than the type of the object being added because there could easily be several different relationships to the same type of object. Any time this is the case, using the object type would create ambiguity.
First of all, it doesn't really make sense to say that there is a one-to-one relationship from DataStore to Test and a many-to-one the other way.
Most likely DataStore is able to hold references to many Test instances, while Test can only have one reference to a DataStore instance. In this case you have a one-to-many relationship from DataStore to Test and a many-to-one relationship from Test to DataStore. Correct me if I'm wrong.
If I got your modelling correct, DataStore will contain an NSSet property (probably) called tests and Test will contain a DataStore property (probably) called dataStore.
You set the relationship with those properties, and it can be done either way. You can say:
DataStore *dataStore = // get instance of DataStore
Test *test = // get instance of Test
// Possibility one - add Test to DataStore
[dataStore addTestsObject:test];
// Possibility two - add DataStore to Test
[test setDataStore:dataStore];
// ... or you can use dot notation
test.dataStore = dataStore;
精彩评论