I am having two objects in my model: Person and Address. Person has a key name and a relationship to Address and Address has a key street.
I have designed the user interface to show a table with one column (Person.name) and two text fields with name and street.
I have created one NSArrayController with managedObjectContext set and ObjectController set to Entity and EntityName Person.
Running the app the table shows all added persons. The two text fields are connected to the Person-ArrayController via Bindings tab:
nameField with value:
Bind To: Person / Controller Key: selected / Model Key Path: name
streetField with value:
Bind To: Person / Controller Key: selected / Model Key Path: address.street
I am able to write something into name and it is stored persistently. I am also able to enter something into streetField but it is not stored.
Does anyone know why??
When the array controller creates the person object, something similar to below code happens:
(assume moc is the AppDelegate managedObjectContext,also assume Person is the sub-class of NSManangedObject):
If
personObj.addressis logged , nil will be printed on the console.This is because the
personObj.addressis anAddressobject which till now has not been instantiated. ConsequentlypersonObj.address.streetwill beNULL.This is what happens in your case also.
if you change the above code to the following it will work fine.
and this can’t be done using binding.
In short
streetFieldbound toaddress.streetdoesn’t get any value becauseaddressdoesn’t point to an instantiated object.