LeadZeppelins wrote:
I created a simple class that stores data, bound its properties to textfields ...
I'm confused about the relationship between the bindings and registering an observer. Are you adding the code you described in addition to bindings defined in IB? Can you reproduce the observer problem when there are no bindings involved? If so, I'd recommend these (mostly generic) trouble shooting steps:
1) Immediately prior to your addObserver call, log the value of the receiver as well as the four params. I would recommend NSLog() over the debugger for this step. When learning Cocoa, one of the most common mistakes is passing a message to a nil receiver, since there will be no run time warning. Also be sure addObserver isn't being passed to a second instance of your class which is not the active instance you meant to observe. Atm I don't know what happens when ofObject is nil, but that's probably self, so not a likely mistake.
2) In case you haven't already done so, copy and paste the declaration of observeValueForKeyPath directly from the KVO protocol ref. to replace that line in your implementation.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change
Since this method is implemented in NSObject, you won't get a run time error if there's a typo in the signature of your implementation;
3) Also add NSLog() to the above to rule out any dependence on subsequent execution (e.g. don't conclude that failure to update some control means that observeValueForKeyPath hasn't run);
4) If none of the above helps, build the smallest possible test bed needed to demo the failure. If that test bed doesn't give you a clue, post it here.
Hope that helps!
- Ray