Episode #152

Cocoa Bindings

13 minutes
Published on January 15, 2015

This video is only available to subscribers. Get access to this video and 586 others.

In this episode I take a look at the basics of Cocoa Bindings. With Bindings you can have your controls on your view bound to properties on your controller or model, and even have controls bound to themselves. What would normally be a lot of manual plumbing code is handled for you automatically by bindings. To demonstrate, we build a live-updating temperature converter.

Episode Links

The Converter Object

+ (NSSet *)keyPathsForValuesAffectingCelsius {
    return [NSSet setWithObject:@"fahrenheit"];
}

- (CGFloat)celsius {
    return (self.fahrenheit - 32) / 1.8;
}

- (void)setCelsius:(CGFloat)celsius {
    self.fahrenheit = (celsius * 1.8) + 32;
}

- (void)setNilValueForKey:(NSString *)key {
    if ([key isEqualToString:@"celsius"] || [key isEqualToString:@"fahrenheit"]) {
        [self setValue:@0 forKey:key];
    } else {
        [super setNilValueForKey:key];
    }
}

@end