Skip navigation.
 
mlBindings + contents of an array. No dice, for newb.
FROM : Shamyl Zakariya
DATE : Tue Apr 03 23:47:50 2007

I'm new to bindings, and I can't figure out how to get it to notice 
changes to the elements contained in an array. So I've written a very 
very simple app to try to figure out what's going on.

1) I have an object "Thing" which has an array of "Value" instances 
( "values" ), where each Value has a single string property, "value".

2) Thing has a method "output" which returns a string which is the 
comma separated concatenation of all the Value instances' -value 
strings.

The Thing -output method looks like so:
- (NSString *) output
{
   NSString *output = nil;    
   NSEnumerator *enumerator = [_values objectEnumerator];
   Value *v = nil;
   
   while( v = [enumerator nextObject] )
   {
       if ( output )
           output = [output stringByAppendingFormat: @", %@", [v value]];
       else
           output = [v value];
   }

   return output;
}


3) The constructor of Value sets its value to "ValueN" where N is an 
increasing number ( e.g., Value0, Value1, Value2.. )

4) Finally, in Thing's +initialize I set the following dependency to 
make -output dependant on -values:
+ (void) initialize
{
   [self setKeys:
       [NSArray arrayWithObjects:
           @"values",
           @"values.value",
           nil]
       triggerChangeNotificationsForDependentKey: @"output"];
}

In IB I made an NSObjectController "ThingAlias" who's content is 
bound to an instance of Thing. I then bound a read-only text field to 
ThingAlias.selection.output. Then I made an NSArrayController 
"Values" and bound it to ThingAlias.selection.values, and bound a 
table view with one column to display Values.arrangedObjects.value. 
Finally, I have +/- buttons to add and delete the entries in the array.

So, when I run this, each time I hit "+", the result field shows 
"Value0", then "Value0, Value1", then "Value0, Value1, Value2" and so 
on. This is what I expect. Hitting "-" removes the newest from the 
array and the output field reflects this.

Now, when I double click one of these Value instances, and type a new 
name, I'd expect on committing it ( hitting enter ) to see the output 
field updated. But it doesn't!

However, if I hit "+" or "-", the dependancy is triggered and I see 
the renamed value show up in the results field. So, clearly changing 
the *array* triggers the notification, but changing the values in an 
object owned by the array does not.

How can I get bindings to notice changes to the contents of the 
array, not just the array instance itself?

<email_removed>
    "It's all malarky; even the wonderful part is malarky"
        --Robert Stack

Related mailsAuthorDate
mlBindings + contents of an array. No dice, for newb. Shamyl Zakariya Apr 3, 23:47
mlRe: Bindings + contents of an array. No dice, for newb. Shamyl Zakariya Apr 4, 14:23
mlRe: Bindings + contents of an array. No dice, for newb. Mike Abdullah Apr 4, 16:21
mlRe: Bindings + contents of an array. No dice, for newb. Shamyl Zakariya Apr 4, 19:26