views:

325

answers:

2

I'm using Cocoa bindings to manage a table of objects. I understand how bindings work but I've run into a slight problem. Managing the table of objects would be fine and dandy, except that those objects have to manage actual bluetooth hardware. I'm working off of a framework that provides a class representing a connection to this hardware, and have made another "manager" class the makes it key-value compliant. In other words, this manager class has to be able to connect and modify its "connect" status in its properties dictionary, be the delegate of this hardware and modify properties, and update the hardware with changes made.

However, whenever I set new values within the object itself, like in a "connect" method that would change the "connect" key's value to 2 (looking), (i.e. propertiesDict = newDict), the change is not seeming to be picked up by observers that it is bound to. I've looked at the observeValueForKeyPath:ofObject:change:context: in the NSKeyValueObservingProtocol. However, I don't know what to do with the context argument.

I hope that makes sense... but if anyone has any ideas I'd love to hear them.

+2  A: 

Your question isn't totally clear, but if I'm understanding it correctly the issue might be because you need to send manual KVO notifications before and after you change a value in the embedded object. For instance, [self willChangeValueForKey:@"connected"]; and [self didChangeValueForKey:@"connected"];.

Marc Charbonneau
I agree. It sounds like you're assigning a new value to the instance variable representing a property from outside the setter for that property. In that case, the KVO notifications will not be sent automatically. Cocoa is very smart, but not smart enough to recognize that this assignment amounts to a change to a key value. You will need to bracket the assignment to the instance variable with willChangeValueForKey: and didChangeValueForKey:
Alex
A: 

Exist three ways of update a property/attribute in a KVO compatible way:

  1. using the seater of the property (specified in the '@property' or generated by '@synthesize')
  2. calling willChangeValueForKey: and didChangeValueForKey: beefor and after you change the property value in any way
  3. calling setValueForKey:
GojaN