Love/hate relationship with Cocoa Bindings

I have a love/hate relationship with Cocoa Bindings. On the surface, bindings look really cool. Write less code and get a program running faster. This is great in theory, but in reality, it can be tricky. Bindings in Interface Builder work OK (however, there seems to be annoying Interface Builder bugs that change the focus when you try to bind an object). The problem arises when you remove an object from a nib, run the app and find out that your get an exception that the object is not key/value compliant. So, you have to search through the nib looking for the bindings that are no longer relevant. If you have a lot of objects, this can be time consuming and problematic.

On the love side, I can more easily bring up a user interface and then have things update automatically when something else changes.

In order to help troubleshoot these loose bindings, I found a cool debugging technique. First, in your app’s initialize method, add the following:

+ (void) initialize
{
	[BindingDebugObject poseAsClass:[NSObject class]];
}

then add the debugging object:

@implementation BindingDebugObject
- (void)bind:(NSString *)binding toObject:(id)observable
	withKeyPath:(NSString *)keyPath options:(NSDictionary *)options
{
	NSLog(@"Binding: %@ %@ %@ %@", binding, observable, keyPath, self);
	[super bind:binding toObject:observable withKeyPath:keyPath options:options];
}

- (void)unbind:(NSString *)binding
{
	NSLog(@"unbind: %@ %@", self, binding);
	[super unbind:binding];
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.