TweetFollow Us on Twitter

The Road to Code: Writing Less Code

Volume Number: 24 (2008)
Issue Number: 09
Column Tag: The Road to Code

The Road to Code: Writing Less Code

The Road to Code: Writing Less Code

by Dave Dribin

Key-Value Coding

In last month's article, I discussed the model-view-controller (MVC) design pattern and how the Cocoa framework adopts it. Figure 1 summarizes the interactions between the model, view, and controller in the MVC pattern.


Figure 1: Model-View-Controller interactions

We also looked at our simple single-window rectangle application through the MVC prism. The view portion of our application is handled by classes in the AppKit framework: NSWindow, NSTextField, NSButton, NSApplication, etc. We were able to reuse these Cocoa view objects without modifying them. The rest of the application was written using two custom classes: the Rectangle model class and a controller class called HelloWorldController. The controller class was designed using the Mediator design pattern to keep the view and model classes in sync. It responded to the button action to synchronize the model with the view.

The controller class is relatively simple code; it's basically just shuttling information back and forth between the model and view. In fact, as you start to write more applications in Mac OS X, you'll find that a lot of controller code is similar from application to application. While this does make it easier to write, as you gain more experience, it also means you spend a lot of time writing repetitively similar, boring code. This means there's more code you have to keep bug-free, which also means you're not spending your time adding new features to your application.

Introduced in Mac OS X 10.3, Cocoa bindings is a technology to help reduce and sometimes completely eliminate the amount of controller code you have to write. The idea is that Cocoa provides programmers with reusable controllers that you can you use in your application, just as the Cocoa frameworks comes with many reusable views. There's just one catch. Your models have to be written using a convention called key-value coding.

Key-Value Coding

Key-value coding, or KVC for short, affects both the authors of model classes and the users of model classes. As an author of model classes, or someone who creates model classes, you must follow certain conventions. Fortunately, we've already been following these conventions. Remember when we were first designing our Rectangle class, we used accessor methods to expose instances variables as part of good encapsulation. Thus, to expose the _width instance variable to allow the user of this class to read and write the width, we needed to have two accessor methods, a getter and a setter:

- (float)width;
- (void)setWidth:(float)width;

The getter method returns the width of a rectangle and the setter method allows someone to set the width of a rectangle. By convention, this pair of methods represents the "width" property of a rectangle. Getter methods have the same name as their property and setters methods are named set<Property>:. On Leopard, we can use the new @property syntax to simplify writing getter and setter methods like this:

@property float width;

Since we're going to be working with the Rectangle model class a bit, I'm including the interface and implementation of it in Listing 1 and Listing 2. I have removed the NSCoding support to help us focus on KVC.

Listing 1: Rectangle.h interface

#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
{
    float _leftX;
    float _bottomY;
    float _width;
    float _height;
}
@property float leftX;
@property float bottomY;
@property float width;
@property float height;
@property (readonly) float area;
@property (readonly) float perimeter;
- (id)initWithLeftX:(float)leftX
           bottomY:(float)bottomY
            rightX:(float)rightX
              topY:(float)topY;
@end

Listing 2: Rectangle.m implementation

#import "Rectangle.h"
@implementation Rectangle
@synthesize leftX = _leftX;
@synthesize bottomY = _bottomY;
@synthesize width = _width;
@synthesize height = _height;
- (id)initWithLeftX:(float)leftX
           bottomY:(float)bottomY
            rightX:(float)rightX
              topY:(float)topY
{
    self = [super init];
    if (self == nil)
        return nil;
    
    _leftX = leftX;
    _bottomY = bottomY;
    _width = rightX - leftX;
    _height = topY - bottomY;
    
    return self;
}
- (float)area
{
    return _width * _height;
}
- (float)perimeter
{
    return (2*_width) + (2*_height);
}
@end

So why bother with this KVC naming convention? It not only makes reading and writing code easier, but more importantly, it also enables us to access properties dynamically by string names. All objects that inherit from NSObject have a method named valueForKey: with the following signature:

- (id)valueForKey:(NSString *)key;

They are using the word "key" here, but the key is just a property name. You'll often see "key" and "property" used interchangeably in regards to KVC. They are not always the same, but they usually are. This method allows you to retrieve a property by name. So, instead of writing code to access the width property using a method or property dot notation:

    Rectangle * rectangle = ...;
    float width = rectangle.width;
    // OR: float width = [rectangle width];
    NSLog(@"width: %.1f", width);

you could access the width property like this:

    NSNumber * width = [rectangle valueForKey:@"width"];
    NSLog(@"width: %@", width);

There are a couple of quirks here. First, because valueForKey: returns an id type, only Objective-C objects may be returned. In order to satisfy this requirement, all primitives, such as float and int, are returned as NSNumber objects. Also, the property name is passed as a string. This defeats the static checking of the compiler. If I mistype the "width" string as "wdith", the compiler does not warn me, in contrast to mistyping the width property or method name.

Properties may also be set using the setValue:forKey: KVC method:

- (void)setValue:(id)value forKey:(NSString *)key;

Again, instead of using the setter method or property dot notation:

    rectangle.width = 8;
    // OR: [rectangle setWidth:8];
you could set the width property like this:
    [rectangle setValue:[NSNumber numberWithFloat:8]
                 forKey:@"width"];

And again, we have to wrap the primitive value in an object because value is of type id and we use a string for the property name. Yes, KVC is more typing and less type-safe than using methods or properties, but it does have its advantages, as you will soon see.

Key Paths

Let's say, for instance, that we have a House class that represents its door as a Rectangle:

@interface House : NSObject { ... }
@property (readwrite, retain) Rectangle * door;
@end

Using traditional property accessor, we could get the door's width like this:

    House * house = ...;
    float width = house.door.width;
    // OR: float width = [[house door] width];

If we want to get the door's width directly using KVC, we need to use a different method, valueForKeyPath: with the signature:

- (id)valueForKeyPath:(NSString *)keyPath;

This would be used as follows:

    NSNumber * width = [house valueForKeyPath:@"door.width"];

Why a different method? Well, we are accessing two properties at once, first door and then width. Using valueForKey: assumes the key name is a single property name. A key path, on the other hand, is a list of properties separated by a dot, in a very similar fashion to the property dot notation. Thus the key path "door.width" accesses the property named "door" on the first object and then "width" on the second object. So using a key path is just shorthand for repeatedly calling valueForKey:

    NSNumber * width = [[house valueForKey:@"door"]
                        valueForKey:@"width"];

You can also set values by key path using the setValue:forKeyPath: method:

- (void)setValue:(id)value forKeyPath:(NSString *)keyPath;

For example, to set the door's width using a key path:

    [house setValue:[NSNumber numberWithFloat:8]
         forKeyPath:@"door.width"];

So all in all, using KVC to access keys and key paths is an awkward way to access properties. I mean, why bother with all of this? If you don't know the name of the properties at compile time, then using strings to access properties is a nice alternative. But why would you not know the name of a property at compile time? The answer to this question is Cocoa bindings.

Cocoa Bindings

I briefly mentioned that Cocoa bindings allowed us to use controller classes supplied by Apple to replace much of our custom HelloWorldController class. Before going further, let's look at the code for the controller, without the use of Cocoa bindings. The interface and implementation are shown in Listing 3 and Listing 4.

Listing 3: HelloWorldController interface before bindings

#import <Cocoa/Cocoa.h>
@class Rectangle;
@interface HelloWorldController : NSObject
{
    IBOutlet NSTextField * _widthField;
    IBOutlet NSTextField * _heightField;
    IBOutlet NSTextField * _areaLabel;
    IBOutlet NSTextField * _perimeterLabel;
    
    Rectangle * _rectangle;
}
- (IBAction)calculate:(id)sender;
@end

Listing 4: HelloWorldController implementation before bindings

#import "HelloWorldController.h"
#import "Rectangle.h"
@interface HelloWorldController ()
- (void)updateAreaAndPerimeter;
@end
?@implementation HelloWorldController
- (id)init
{
    self = [super init];
    if (self == nil)
        return nil;
    
    _rectangle = [[Rectangle alloc] initWithLeftX:0
                                       bottomY:0
                                        rightX:5
                                         topY:10];
    
    return self;
}
- (void)awakeFromNib
{
    [_widthField setFloatValue:_rectangle.width];
    [_heightField setFloatValue:_rectangle.height];
    [self updateAreaAndPerimeter];
}
- (IBAction)calculate:(id)sender
{
    _rectangle.width = [_widthField floatValue];
    _rectangle.height = [_heightField floatValue];
    [self updateAreaAndPerimeter];
}
- (void)updateAreaAndPerimeter
{
    [_areaLabel setFloatValue:_rectangle.area];
    [_perimeterLabel setFloatValue:_rectangle.perimeter];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:
    (NSApplication *)application
{
    return YES;
}
@end

Our controller currently has a number of responsibilities. It creates and holds onto a single instance of our Rectangle model class and sets up a reasonable default dimensions for the rectangle in its constructor. It is also the NSApplication delegate, and ensures the application quits if the window is closed. The rest of the code in awakeFromNib, calculate: and updateAreaAndPerimeter keep the view and the model in sync. It's these three methods that we are going to replace with Cocoa bindings.

Apple's controllers inherit from the class NSController. There are different subclasses, but the one we are interested in is called NSObjectController that is a designed for a single model object. Since our application's controller was previously dealing with a single instance of the Rectangle class, this is the kind of controller we want.

To start converting our application to use Cocoa bindings, we first need to make the rectangle instance of our controller class available to other objects. Currently, it's only an instance variable, but we want an NSObjectController to become the controller for it. Let's expose this instance variable as a property by adding a @property line to our interface:

@property (readonly) Rectangle * rectangle;

Let's also add a @synthesize line to our implementation:

@synthesize rectangle = _rectangle;

This not only exposes the rectangle instance variable, but it also creates a KVC compliant getter method. This means the this property can be accessed using valueForKey:, as above.

Our next step involves Interface Builder, so open up MainMenu.nib. It should look like Figure 2.


Figure 2: Original MainMenu.nib

The important part to notice is that we have an instance of our HelloWorldController. We're going to add an instance of NSObjectController to our nib. Type "nsobjectcontroller" into the Library window's filter field to find it, as shown in Figure 3.


Figure 3: NSObjectController in Interface Builder

Now drag this over to the nib window to create an instance of this class in our nib. Next, open up the Attributes tab of the Inspector window. You'll notice a text field called Class Name. This field is the class name of the attribute for which this is a controller. Change NSMutableDictionary to Rectangle since that is the name of our model class. Also, uncheck the Editable checkbox. The final result is shown in Figure 4.


Figure 4: NSObjectController attributes

I also suggest renaming the instance of the object controller to Rectangle Controller in the nib window, as shown in Figure 5. This will make it easier to remember the purpose of this particular controller instance.


Figure 5: Renamed object controller instance

We now need to hookup the rectangle controller to the rectangle instance of our HelloWorldController. NSObjectController has a property named content that refers to the object that it controls, so we need to set this to our rectangle instance. We could do this in code in our awakeFromNib method by creating an outlet to the object controller and calling its setContent: method, but it turns out we can set this up without any new code using Cocoa bindings.

With the Rectangle Controller still highlighted, switch to the Bindings tab of the Inspector window (it's the fourth icon from the left). Open the disclosure triangle for Content Object, and you'll be presented with a bunch of new fields. From the Bind to: popup, select Hello World Controller. In the Model Key Path field, type "rectangle" in all lower-case and hit Return. The result should look like Figure 6.


Figure 6: Rectangle controller's bindings

Congratulations! You've just setup your first Cocoa binding. We have now bound the content object to the "rectangle" key path of our HelloWorldController instance. So now you can see why accessing properties using strings is useful. Interface Builder stores the "rectangle" string, and somewhere in the bowels of the Cocoa framework, it's going to call valueForKeyPath: on our controller to get the rectangle instance, similar to this code:

    Rectangle * rectangle =
        [helloWorldController valueForKeyPath:@"rectangle"];

It uses whatever you type into the Model Key Path as the string passed to valueForKeyPath:.

Our next step is to use Cocoa bindings on the rest of the controls in this window. Select the text field to the right of the Rectangle Width label in the window, and switch to the Bindings tab of the Inspector window. Open up the disclosure triangle for Value, bind to the Rectangle Controller. Set the Model Key Path to "width." Keep the Controller Key set to "selection." Cocoa bindings is designed to handle objects getting selected and unselected from the user interface. In our case, the selection will always represent the rectangle instance we setup in the Content Object binding. The final result should look like Figure 7.


Figure 7: Rectangle width bindings

Repeat this procedure for the height, area, and perimeter text fields, binding them to the "height," "area," and "perimeter" key paths of the Rectangle Controller, respectively. Also, delete the Calculate button, as we will no longer need this. The final user interface is summarized in Figure 8.


Figure 8: Final user interface

You can now delete the awakeFromNib, calculate: and updateAreaAndPerimeter methods from HelloWorldController and you can delete all the outlets to the text fields. With bindings in place, we don't need any of this code.

If you ran the application right now, you should see the fields filled in with the initial values we setup in the constructor, namely a width, height, area, and perimeter of 5, 10, 50, and 30 respectively. However, there's one problem. If you change the width or height, the area and perimeter do not update. What's going on here?

Key-Value Observing

Before we go over the solution to this problem, we need to dig a little further into how bindings work. Look back at the MVC design pattern in Figure 1. You'll see an arrow from the Model to the Controller labeled as Notify. This means that when the model changes, it's supposed to notify the controller that something has changed. This is handled through KVC's companion technology called key-value observing, or KVO for short.

KVO allows one object to register for changes to key paths of another object. This is similar to how you can register for notifications using NSNotificationCenter. For example, say we wanted our HelloWorldController to be notified whenever the width of its rectangle instance is changed. To register for these changes, NSObject declares a method named addObserver:forKeyPath:.... We could call this in the HelloWorldController constructor as such:

- (id)init
{
    self = [super init];
    if (self == nil)
        return nil;
    
    _rectangle = [[Rectangle alloc] initWithLeftX:0
                                       bottomY:0
                                        rightX:5
                                          topY:10];
    [_rectangle addObserver:self
                 forKeyPath:@"width"
                    options:0
                    context:nil];
    
    return self;
}

This registers our instance of HelloWorldController as an observer to the _rectangle's "width" key path. Unlike notifications, you cannot choose which method gets called when the key path changes. All KVO change notifications call a method named observerValueForKeyPath:... on our object. Here's a simple implementation that just logs the key path:

- (void)observeValueForKeyPath:(NSString *)keyPath
                     fObject:(id)object
                      change:(NSDictionary *)change
                     context:(void *)context
{
    NSLog(@"Key path changed: %@", keyPath);
}

We've now fully setup KVO to watch when the width property of our rectangle changes. If we run the application, and change the width value in the user interface, you should see the log statements in your console output.

Generally, you don't have to use KVO directly, as it's more of a behind-the-scenes technology for bindings. All of Cocoa's controllers, including NSObjectController, uses KVO to be notified of changes to the content object, i.e. the model, and then updates the view with the new values, automatically. Thus, when we bound the area text field to "area" through NSObjectController, it set up a two way street between the text field and the key path. When the value of the text field changes, the text field notifies the controller. The controller then updates the model to the new value. Conversely, when the controller detects the model changed via KVO, it updates the view to the new value.

So how does this relate to area text field not being updated? The problem is that the area value is the width multiplied by the height. Thus, if the width changes, the area is also changed. The same goes for the height. Since Cocoa cannot possibly know the relationship between a rectangle's width, area, and height, we have to tell it.

What we have here is a situation called dependent keys. We have one key, area, that is dependent on two other keys, width and height. To setup dependent key relationships in the model, we need to implement a class method named +keyPathsForValuesAffecting<Key> in our Rectangle class. The implementation for the area key is as follows:

+ (NSSet *)keyPathsForValuesAffectingArea
{
    return [NSSet setWithObjects:@"width", @"height", nil];
}

This tells Cocoa that the width and height keys affect the area key. Since the perimeter is also dependent on width and height, it is also a dependent key and needs its own class method:

+ (NSSet *)keyPathsForValuesAffectingPerimeter
{
    return [NSSet setWithObjects:@"width", @"height", nil];
}

By setting up our dependent keys, our Rectangle model class is now truly KVC compliant. In practice, this changes the way KVO works. Thus, when the width changes, not only does the KVO mechanism send out a notification that the width has changed, but it also sends out notifications that the area and perimeter have changed. Thus, any observers registered for changes to "area" or "perimeter" are now properly notified when either width or height changes.

With these two class methods added to the implementation of our Rectangle class, our application should now work properly. Changing the area or width will automatically update the area and perimeter.

You may still be wondering what the big deal with bindings is. Sure, we were able to delete three methods in the controller, but we had to add two methods to the model. While we're keeping this example relatively short, as you create more complex applications, Cocoa bindings will save you a lot of coding.

Conclusion

We've covered how to use Cocoa bindings instead of custom controller code. Along the way we've learned about key-value coding (KVC) and key-value observing (KVO). We've only scraped the surface of bindings, KVC and KVO. There are a few more hairy details to these technologies, but we've covered the basics. Again, I highly recommend actually working through the steps in this article on your own to convert the pre-bindings application to use Cocoa bindings. If you are having issues getting bindings to work, you can download before and after projects from the MacTech website to compare your project to a functional project.


Dave Dribin has been writing professional software for over eleven years. After five years programming embedded C in the telecom industry and a brief stint riding the Internet bubble, he decided to venture out on his own. Since 2001, he has been providing independent consulting services, and in 2006, he founded Bit Maki, Inc. Find out more at http://www.bitmaki.com/ and http://www.dribin.org/dave/.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.