Upcoming Classes

RSS Feeds

Categories

Archive

Site search

Property List Serialization

Saving data to the file system and reading it back is a pretty common operation. One of the first ways many Cocoa and iOS programmers learn to read and write are by using the convenience functions provided by NSArray, NSData, and friends. NSDictionary has writeToFile:atomically: or writeToURL:atomically: for writing and dictionaryWithContentsOfFile: or dictionaryWithContentsOfURL: for reading.

The convenience calls assume that your data is in property list format (a.k.a. “plist”) and will automatically encode things for later retrieval. As a reminder, property list data means that you have a collection of objects of only these six types, or their mutable counterparts: NSString, NSData, NSNumber, NSDate, NSArray, NSDictionary. You can have an array of dictionaries with string keys and number and date values and still be considered a property list. Any other classes are forbidden. An NSColor or UIBezierPath in the dictionary will make the collection non-property-list.

These default reading and writing methods are kind of lame though. You can’t control the format the data is written – Cocoa does what Cocoa does. There’s also little-to-no error handling in many of the calls, so there’s not much feedback to tell you what’s wrong if someone accidentally (or purposely!) added a little Pony object to the collection when you weren’t looking. You’ll have to encode non-plist objects into a plist type (usually NSData) to play in this playground. Also, mutability is lost. If you save a mutable array containing mutable strings, you’ll get back their immutable counterparts when re-read.

Cocoa has two “property list serialization” classes that work around these problems: NSPropertyListSerialization (that class name is just cool to say), and NSJSONSerialization.

NSPropertyListSerialization

NSPropertyListSerialization takes plist objects and encode them into an NSData, which you can write to a file, or send out a network connection, or something similar. The class can also take plist objects, encode them, and write them out an NSOutputStream.

Going the other direction, you can hand NSPropertyListSerialization an NSData you got from somewhere, or give it an NSInputStream with a stream of bytes from somewhere. If the decoding works, you get a pointer to the top-level plist object of the collection.

There are three formats used by the property list serialization class. There’s the the old “OpenStep” format, which looks a lot like JSON. Not terribly compact, but easy to read and edit. You can only read the OpenStep format – NSPropertyListSeialization doesn’t support writing it. There’s an XML format, and a binary format. The XML format is human-readable and editable while the binary format is not.

Aside: you can convert an existing property list file between formats using the plutil utility. By default, preference property list files are saved in binary format. You can verify that, and then convert it:

% cd ~/Library
% file Preferences/com.apple.finder.plist
Preferences/com.apple.finder.plist: Apple binary property list
% plutil -convert xml1 Preferences/com.apple.finder.plist
% file Preferences/com.apple.finder.plist
Preferences/com.apple.finder.plist: XML  document text

You can open this plist in TextEdit (or your favorite editor) to revel in its XML glory.

These formats correspond to the property list serialization constants NSPropertyListOpenStepFormat, NSPropertyListXMLFormat_v1_0, and NSPropertyListBinaryFormat_v1_0.

Writing Property lists

Ok, so how do you use NSPropertyListSerialization? It’s actually a class with nothing but class methods. You never interact with instances of NSPropertyListSerialization.

To serialize a property list, you need a property list to serialize. The sample plist will be a dictionary with various plist types: (the complete code can be found at this gist)

static id makePlistObjects (void) {
    NSMutableDictionary *top = [NSMutableDictionary dictionary];

    [top setObject: @"Hi I'm a string"  forKey: @"string"];
    [top setObject: [NSNumber numberWithInt: 23]  forKey: @"number"];
    [top setObject: [NSNumber numberWithBool: YES]  forKey: @"boolean"];
    [top setObject: [NSDate date]  forKey: @"date"];

    NSArray *array =
        [NSArray arrayWithObjects: @"I", @"seem", @"to", @"be",
                                   @"a", @"verb", nil];
    [top setObject: array  forKey: @"array"];

    NSDictionary *dict =
        [NSDictionary dictionaryWithObjectsAndKeys:
                      @"Ack", @"Oop",
                      @"Bill the Cat", @"It's", nil];
    [top setObject: dict  forKey: @"dictionary"];

    return top;

} // makePlistObjects

When NSLogged, it looks something like this:

2012-05-13 21:07:24.601 serial[57822:303] plist {
    array =     (
        I,
        seem,
        to,
        be,
        a,
        verb
    );
    boolean = 1;
    date = "2012-05-14 01:07:24 +0000";
    dictionary =     {
        Oop = Ack;
        "It's" = "Bill the Cat";
    };
    number = 23;
    string = "Hi I'm a string";
}

Once you have your property list object you can pre-flight it by using propertyList:isValidForFormat: Here’s the start of a function to save the plist as XML:

static void saveAsXML (id plist) {
    if (![NSPropertyListSerialization
             propertyList: plist
             isValidForFormat: kCFPropertyListXMLFormat_v1_0]) {
        NSLog (@"can't save as XML");
        return;
    }

If you want to find out precisely why it can’t be saved, go ahead and try to encode it anyway and inspect the error that comes back. Usually it will be errors of the form “property lists cannot contain objects of type 'BNRPony'

To actually save in XML format, convert the property list to an NSData:

    NSError *error;
    NSData *data =
        [NSPropertyListSerialization dataWithPropertyList: plist
                                     format: kCFPropertyListXMLFormat_v1_0
                                     options: 0
                                     error: &error];
    if (data == nil) {
        NSLog (@"error serializing to xml: %@", error);
        return;
    }

Notice the error handling – always check the return value from the method, not the NSError. If that sentence doesn’t mean much to you, or you actually test the value of the NSError, then go, now, and read An NSError Error.

The NSData now contains an encoded string which is the complete XML of the encoded property list. The arguments to dataWithPropertyList:... are straightforward. Give it the top-level object of your property list object graph. Then a format constant, then zero for the options (it’s a place holder), and then the obligatory error-return pointer.

Once you’ve got the data, you can write it out. Here it is written to the current directory:

    BOOL writeStatus = [data writeToFile: @"plist.txt"
                             options: NSDataWritingAtomic
                             error: &error];
    if (!writeStatus) {
        NSLog (@"error writing to file: %@", error);
        return;
    }
} // saveAsXML

Most sane people will use something like NSSearchPathForDirectoriesInDomains or URLsForDirectory:inDomains: to get a good place to drop a file.

Saving in the binary format is exactly the same, except you’d use kCFPropertyListBinaryFormat_v1_0.

Reading

Reconstructing your property list objects works the same way, just backwards. You read in the OpenStep, XML, or binary file from the file system, then use an NSPropertyListSerialization class method to convert it into real live objects.

Here’s the counterpart function to saveAsXML: given a file name, read it in and deserialize it. Here’s the reading of the file into an NSData. Standard b-flat stuff:

static id readFromFile (NSString *path) {
    NSError *error;
    NSData *data = [NSData dataWithContentsOfFile: path
                           options: 0
                           error: &error];
    if (data == nil) {
        NSLog (@"error reading %@: %@", path, error);
        return nil;
    }

And now the deserializing:

    NSPropertyListFormat format;
    id plist = [NSPropertyListSerialization propertyListWithData: data
                                            options: NSPropertyListImmutable
                                            format: &format
                                            error: &error];

It’s a bit more complicated, but too much. You give it the data that holds the contents of file, along with some creation options. More on those in a bit. You pass the address of an NSPropertyListFormat so you can find out what the original format was (if that’s important / interesting to you), as well as the address of an NSError pointer to fill in if anything went wrong.

Here’s the error handling, and printing out of the file format:

    if (plist == nil) {
        NSLog (@"could not deserialize %@: %@", path, error);

    } else {
        NSString *formatDescription;
        switch (format) {
          case NSPropertyListOpenStepFormat:
            formatDescription = @"openstep";
            break;
          case NSPropertyListXMLFormat_v1_0:
            formatDescription = @"xml";
            break;
          case NSPropertyListBinaryFormat_v1_0:
            formatDescription = @"binary";
            break;
          default:
            formatDescription = @"unknown";
            break;
        }
        NSLog (@"%@ was in %@ format", path, formatDescription);
    }

And then the plist is returned from the function.

Mutability

So what’s that NSPropertyListImmutable parameter? That parameter controls the kinds of classes that are created when the property list is reconstituted. “Immutable” is the default behavior (if you pass zero). That means that all arrays will be NSArrays, all Dictionaries will be NSDictionaries, all strings will be NSStrings, and so on, even if the original objects were mutable dictionaries, dictionaries, or strings.

You can also pass NSPropertyListMutableContainers to turn all of the arrays and dictionaries into NSMutableArray and NSMutableDictionary instances. There’s also NSPropertyListMutableContainersAndLeaves, which gives you mutable collections, as well as NSMutableStrings and NSMutableDatas. There’s no such thing as mutable NSNumbers or NSDates.

The mutability of these objects is not saved to the plist file. If you’re depending on mutability being preserved when writing to a plist, or something that’s plist-backed (like NSUserDefaults), you’re going to break.

Immutable objects are generally safer (no need to worry about a value changing underneath you), so I prefer using those. But if you want to manipulate the results you can tweak the mutability. If you have an existing hierarchy of plist objects but with the wrong mutability, you can pass it through NSPropertyListSerialization to change it, without ever going to the file system. Granted, you’ll have multiple copies of the data in memory.

The type of the mutability flags is kind of a wart in the API. propertyListWithData:... declares the type of the options parameter as NSPropertyListReadOptions, of which there are no values. NSPropertyListImmutable and friends are actually NSPropertyListMutabilityOptionss. They’re all enums, so it doesn’t really matter, but it’s one of the very few places you can’t trust what’s in the header.

NSJSONSerialization

NSJSONSerialization is like NSPropertyListSerialization, except it emits and consumes JSON rather than XML or binary. It’s a relatively new class, available starting in iOS 5 and Mac OS X 10.7. There are some limitations compared to NSPropertyListSerialization, though. The object to encode has to be a collection – an NSArray or NSDictionary. You can’t use numbers as dictionary keys. You can’t save dates or data. But, you can save [NSNull null]s, which you can’t do with property lists.

The calls are very similar to their property list serialization cousins. You can preflight:

    if (![NSJSONSerialization isValidJSONObject: plist]) {
        NSLog (@"can't save as JSON");
        return;
    }

Turn into an NSData:

    NSError *error;
    NSData *data =
        [NSJSONSerialization dataWithJSONObject: plist
                             options: NSJSONWritingPrettyPrinted
                             error: &error];

JSON serializations has an option for writing – pretty printing it with newlines and indentation. Pass zero to get the more compact form.
After you’ve written and re-read it, you would reconstitute it with:

    id plist = [NSJSONSerialization JSONObjectWithData: data
                                    options: 0
                                    error: &error];

The options flags work differently than with property list serialization. Pass zero to get immutable containers, or bitwise-OR in these flags to pick and choose different features:

    NSJSONReadingMutableContainers = (1UL << 0),  // 0001
    NSJSONReadingMutableLeaves     = (1UL << 1),  // 0010
    NSJSONReadingAllowFragments    = (1UL << 2)   // 0100

Mutable containers gives you mutable arrays and dictionaries. Mutable leaves give you mutable strings. You can get mutable containers and leaves by passing NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves. You can choose to have mutable leaves (NSMutableStrings) and immutable containers, which you cannot do with NSPropertyListSerialization.

Notice that Apple fixed the API wart here. These constants are of type NSJSONReadingOptions, so there isn’t a specific mutability type.

The “allow fragments” option lets the JSON have a non-container being the top-level object coming in.

So, should you actually use these?

Now comes the big question. Should you actually *use* these calls?

I wouldn’t use them for my application’s document storage, unless my document really was nothing but a collection of property list objects. There’s better tech for that like NSCoding / Core Data / sqlite3 / etc.

That being said, property list (and JSON) files can be very handy for auxiliary files. Have a small database of stuff? Put it into a property list. You can edit it “for free” with the plist editor found in Xcode (or the stand-alone version). Have something configurable in your program? You can put its setting into a plist file, and then you can edit the file without having to recompile your program. (But you might want to use NSUserDefaults for that) The JSON calls make it pretty easy to pack stuff before sending to a web service, and unpacking the result.

Actually finding a memory management error with Instruments

Instruments is a very cool profiling application, but it’s one of those things that’s kind of hard to write about. You can outline the features it has, create some contrived debugging situations (“oh look, I’ve introduced a memory leak where no sane person could have accidentally created one.”), and make some pretty screen shots.

But then when you actually use it in the real world for solving a problem or coming to some interesting conclusion about the behavior of your system you typically don’t have the time to write about it. And when you finally have some writing time, you’ve already landed the fix to your source code and moved on with life. Then you have to re-introduce the bug, or ratchet back your source code control system when you want to write about it. Or as I’ll write about One Of These Days, the entire premise you were going to write about has changed due to implementation differences under the hood.

Luckily, last time I used Instruments I took screen shots, so here’s a quick example of actually finding a problem quickly with the tool.

The Scenario

A friend of mine is working on a product that’s oh-so-close to shipping, but it has a memory error. Memory grows when you go between two view controllers. You’d push one on to the stack, play with it, pop back, but the view controller was still hanging around in memory. The static analyzer didn’t complain about anything. Glancing at the code didn’t show any obvious memory management errors. Unfortunately this is a central part of the UI, so it’s not one of those “oops, there’s a memory leak in the About Box” situations where you can skip it and work on something more important.

“My memory usage grows” triggers my Pavlovian response of “run Instruments with the Leaks template”. Memory hanging around after it should be cleaned up calls for heapshots, one of my favorite Instruments features. Heapshots show you the objects that are still alive since the last time you took a heapshot. The usual pattern is get the app to a steady state, take a heapshot. Take another to make sure you’re not leaking when the app is idle. Then exercise the problem. Take another one to see who’s still hanging around in memory. Repeat until you understand what’s going on.

This time I was a little lazy. The original bug reporter is an amazingly smart guy who’s opinions I trust, and so I believed something really was not getting cleaned up. I just took a baseline, exercised the problem twice, and took another heap shot: (Click on images for full-sized versons in a new window / tab)

Two heap shots

I expanded the heapshot There are the two SWLGuestActionVCs I expected:


Heapshot Detail

The focus button (the little arrow in a circle thing) by one of the view controllers took me to a detailed memory management history, outlining every retain, release, and autorelease, along with the reference count:


Memory management details

Whoa. that’s a whole lot of stuff. Needless to say, I didn’t read all of it. I just skimmed down the right-hand column looking for something interesting. I just ignored anything with a leading UI because there’s a lot of activity moment-by-moment. After glancing down a couple of screenfulls, this timer jumped out at me:


4 timer dude sm

Ordinarily you don’t get timers for free. And from what I knew about the product, this particular view controller had a timer that drove updates.

It took about three minutes in instruments to get here. But now I actually have enough information to figure out what’s going wrong. Hint: it’s a retain cycle.

The timer is created in viewDidLoad:

    _refreshTimer = [[NSTimer scheduledTimerWithTimeInterval: 5.0
                              target: self
                              selector: @selector(refreshDisplayFromServer:)
                              userInfo: nil
                              repeats: YES] retain];

The retain really isn’t necessary because the runloop retains the timer until it’s invalidated, but doesn’t hurt anything so long as it’s balanced (which it is). The problem is this: the timer retains self, because that’s what it does. The view controller needs to release and invalidate the timer before it can finally get itself cleaned up, so there’s a “Goodbye Kiss” moment when the view controller knows it doesn’t need the timer, and then disposes of it. Otherwise the timer’s retain will keep the view controller alive until the end of time.

OK, the view controller needs to get rid of the timer. And indeed it has code to do that. The timer is cleaned up in viewDidUnload:

- (void) viewDidUnload {
    // release views and other things like good citizens

    [_refreshTimer invalidate];
    [_refreshTimer release];
    _refreshTimer = nil;

    [super viewDidUnload];
} // viewDidUnload

Looks OK to me. The problem is, viewDidUnload was not getting called. Setting a breakpoint confirmed that. viewDidUnload is documented as being called in low-memory situations, and this app is frugal with memory. But in general you can’t trust viewDidUnload for doing generic “view went away” work. The fix for this app is to dispose of the timer in viewDidDisapper:, since that will definitely get called as soon as the user pages back. And of course the timer creation code got moved to viewWillAppear:

But Doesn’t Instruments Detect Retain Cycles?

Instruments will detect retain cycles for you in some cases. I wrote a little app to make retain cycles, and Instruments detected them:


Retain cycle sm

It even has a cool little block-and-arrow diagram showing the relationship between the objects. You can find this under the Cycles & Roots option of the Leaks instrument. Unfortunately, the cycle caused by the timer was not picked up by Instruments. Probably because it’s not actually a leak – the timer still had a reference to the view controller.

That’s It

So what’s the point of this whole discussion? Basically, that it’s possible to use Instruments to find a problem quickly – it’s not just for big boil-the-ocean let-me-find-all-of-my-serious-performance-problems work. In this case, it was a little bit of information and a little bit of experience that pointed to a solution in under ten minutes. It’s worth the time to play around with Instruments and get familiar with the basic features of the different instruments and templates, and then actually use them when faced with problems to solve.

BOOL’s sharp corners

Objective-C is actually a pretty old language, dating back from from the mid eighties. As such, it’s got some sharp corners here and there due to limitations of early C. Today’s sharp corner is BOOL.

BOOL seems innocuous enough. It holds a boolean value. We All Know that in C a zero is a false value, and non-zero is a true value. There’s even the handy YES and NO macros to represent truth and untruth. It turns out that BOOL is actually just a typedef for a signed char – an eight-bit value. It’s not a first-class boolean type, which is one with compiler support (like bool) to make sure it acts sanely. There’s a couple of places where BOOL‘s non-first-classitude can cause subtle problems.

Don’t Compare to YES

YES is Objective-C’s truth value, equal to the value 1. BOOL can actually hold any non-zero value from -128 through 127 (in addition to zero), so there are 255 different flavors of truth. It’s a nice convention that functions and methods always return YES or NO, but you can’t depend on a truth value from one of these to be equal to YES (the value 1). Comparing to YES is generally a bad idea:

if ([kitteh haz: cheezburger] == YES) {
    // LOL
}

You have to trust that -haz: returns YES or NO, and no other value. Without looking at the source or disassembly I can’t trust its truth value is always YES. There is no compiler enforcement that a BOOL return value, or variable, always holds NO or YES.

How could a non-YES value find its way coming out of a function? A common C idiom (not necessarily ideal, but common) is to do some kind of math and then use that value to determine a Truth. If the math results in zero, then the Truth is false. If the math results in a non-zero value, then the truth is True.

Here’s a contrived piece of logic – given two integers, indicate if they’re different. An experienced C programmer might do this:

static BOOL different (int thing1, int thing2) {
    return thing1 - thing2;
} // difference

(You can find this code, and other code referenced here at this gist)

If you used a bool return type this function would actually work correctly.

But with Objective-C, the BOOL return value is actually cast to a char and returned, so it will be the result of the subtraction modulo 256. Comparing to YES will end up causing false negatives.

Here are some uses of this function, comparing to YES:

if (different(11, 10) == YES) printf ("11 != 10\n");
else printf ("11 == 10\n");

if (different(10, 11) == YES) printf ("10 != 11\n");
else printf ("10 == 11\n");

if (different(10, 15) == YES) printf ("10 != 15\n");
else printf ("10 == 15\n");

if (different(512, 256) == YES) printf ("512 != 256\n");
else printf ("512 == 256\n");

You’d hope that all of these would print out “thing1 != thing2” in all cases. But that’s not the case:

11 != 10
10 == 11
10 == 15
512 == 256

Only the first case correctly says that the two numbers are different. If you only had the first case in your unit test, you’d think that things were working correctly. (ship it!)

The actual return values from different are 1, -1, -5, and 0. Only the first one happens to equal YES. And notice the last expression actually evaluated to NO. More on this weirdity in a bit.

Because I don’t trust the general programmer population to be aware of this subtlety when using BOOL, I use this idiom for checking BOOL values:

if ([kitteh haz: cheezburger]) {
    // LOL
}

Of course, it’s always safe to compare against NO, because there is only one false value in C expressions – zero.

But you can rely on logical expressions

When I write code that returns a BOOL, I return a YES or NO value explicitly, versus doing some kind of arithmetic. You can also rely on logical expressions, though, to return values of zero and one, which happen to map to NO and YES.

I was actually caught unawares by this behavior, and was a good Learning Experience for me. I was doing a code review at Google, and saw this code:

BOOL something () {
    // stuff stuff stuff
    return a == b;
}

And I dinged it, saying the return should be return (a == b) ? YES : NO;, because I thought the value of the expression was undefined, and would just evaluate to a C truth or false value. (I blame some buggy compilers in my distant past.)

One of my gurus there, David Philip Oster, countered that it was actually legal, and the value of logical expressions is well-defined. When DPO makes a statement like that, he’s usually correct. So after a couple of minutes of writing some test code and finding chapter and verse in the C99 and C++ standards, I was convinced: logical expressions in C and C++ will always evaluate to a bool true or false value, which are one and zero, and happily equal to YES and NO.

I still won’t ever compare directly to YES because I don’t have time to review everyone’s code I call.

Slice of Life

Another BOOL sharp corner is a variation of the above. Not only can a non-NO BOOL have a non-YES value, sometimes it can be NO.

That sounds scary. How can that happen?

BOOL is a char, which is eight bits. If you try to squeeze a value larger than a char through BOOL, the compiler will happily truncate the upper bits, slicing them off.

So, what does this code print?

    BOOL truefalse = (BOOL)256;
    printf ("256 -> %d\n", truefalse);

Zero. NO. This “true” value is actually zero. Why? Here it is bitwise:

Slice

The compiler happily stripped off the upper byte, leaving zeros. Granted, a casting of a large constant into a BOOL might raise a red flag. But then again, the different function returned the value of a subtraction, truncating the value silently and didn’t require a cast.

The same code, but using a standard bool type works fine:

    bool stdTruefalse = (bool)256;
    printf ("256 -> %d (bool)\n", stdTruefalse);

So if your entire C boolean experience has been with with first-class types like bool (lower-case), then the BOOL (upper-case) behavior probably comes as a surprise.

The same thing can happen with pointers. (Thanks to Mike Ash for introducing me to this class of errors.) You might think this is safe code:

static NSString *g_name;

static BOOL haveName () {
    return (BOOL)g_name;
} // haveName

If g_name is non-nil, it’s a true value, so this function will be returning a true value. It might not be YES, but truth is truth in C, isn’t it? If this function returned bool, it would return the correct value.

Unfortunately, with BOOL, it doesn’t. If the address happens to have a zero lower byte, this function will return zero due to the same slicing behavior.

Here’s two cases. The first is the string living at address 0x010CA880.

Slice 1

That returns a true value, 0×80. It’s not YES, but it’s true. So code in general still works. If the address happens to be aligned in memory such that the lower byte is zero, say 0x010CA800, the compiler would slice off the top three bytes leaving a zero:


Slice 3

So you can see the function works in most cases, except for those times when the string happens to lie at particular addresses. It only fails every now and then. This is the kind of bug that can change from run to run.

Luckily clang and current versions of gcc complain if you try to pass a larger integer or pointer through a BOOL, requiring a cast. Hopefully adding the cast will raise some red flags in the programmer writing the code.

The take-away from this? For this kind of test I would either return an explicit YES or NO value:

if (g_name) return YES;
else return NO;

or use a logical expression

return g_name != nil;

And not depend on automatic pointer->BOOL (or any integer->BOOL) behavior.

Going BOOLing

So, what’s the point of all of this? Mainly that us as users of Objective-C, we can’t forget the C portion of the language. There are the occasional sharp corners that come from Objective-C’s C heritage that we need to be aware of, such as BOOL being a signed char. Many situations that work correctly with bool, the first-class boolean type, can fail in weird and wonderful ways with BOOL.

First Annual Clash of the Coders

Special guest post by LeAnn Boucher, Ranch Public Relations.

We interrupt your regular Big Nerd Ranch Blog programming, stuffed with expert tips and teeming with hands-on advice, to bring you this behind the scenes interview with Big Nerd Ranch’s Most Bad-Ass Coders. (dramatic pause)

Clash Of The Coders Trophy Of AwesomeWe sat down with the winners of the inaugural Clash of the Coders, a 48-hour internal hackathon staged to explore new technology, develop cool apps and test how much coffee a Nerd can possibly consume. Spoilers! Let’s just say that a cottage industry harvesting extra bold beans sprouted up outside our office.

First, the introductions. Team Franklin, harnessing the raw brainwaves of Bill Phillips and Chris Stewart, earned the coveted Big Nerd Ranch’s Most Bad-Ass Coders’ title with their project Roger.

Franklin, Roger, Bill and Chris. Let’s start there.

BNR Blog: First, congratulations Team Franklin! We can’t help but notice a little naming trend. How did you come up with the name Roger?

Franklin: Well, after selecting Franklin as our team name, we knew we needed another proper name for our project. After tossing around names of people we didn’t know, we decided on Roger. Neither of us knew a Roger, so that settled it.

BNR Blog: Uhmmm doesn’t Bill have an Uncle Roger?

Franklin: Yes.

BNR Blog: Have either of you participated in a hackathon before?

Franklin: No, this was our first, and possibly our last, competition. We want to rest on our track record of winning 100 percent of the hackathons we’ve competed in. We really enjoyed the process and had an idea that we were really excited about. That gave us a lot of confidence going into the competition.

BNR Blog: So tell us a bit about Roger.

Franklin: Roger is an Android app that…

BNR Blog: Wait, an Android app won the 2012 Big Nerd Ranch Clash of the Coders?

Franklin: Yes.

BNR Blog: That’s a bit of a surprise! Were there other Android apps?

Franklin: There was one other Android gem nestled in amongst the 11 iOS/OS apps that emerged from Clash of the Coders.

BNR Blog: What does Roger do?

Franklin: Chris came up with the idea before Clash. It’s a developer tool that helps you design the user interface of an Android app. Every time you save a layout file (a file that describes your app’s UI) on your computer, Roger picks up on that save and shoots the file out to any Android devices that are paired with your computer. You can see the UI changes on many different devices in real time with no effort on your part. This is especially important on Android because there is a lot of variation between devices. There are many different screen sizes and themes.

BNR Blog: Wow. That’s a really good idea. Will it ever be available to developers?

Franklin: Aaron loves the idea of open sourcing it as a developer tool. Check it out at https://github.com/bignerdranch/Roger.

BNR Blog: So you had a great idea. What was next?

Franklin: Next we researched what we could realistically do within 48 hours. We entered Clash of the Coders with a strong idea backed by a feasible execution plan. It gave us a lot of confidence going in.

BNR Blog: Were there a lot of sleepless moments?

Franklin: Actually, no! We would leave around 11:30 p.m./midnight, go home to sleep and return by 8:30 a.m. We were in a zone though, oblivious to a lot of what was happening around us.

BNR Blog: How did you keep your focus during the long stretches?

Franklin: Music played a key role – tunes that were repetitive and drifted into the background.

Also, gummy bears. Lots and lots of delicious gummy bears.

BNR Blog: What’s that they say about the best-laid plans?

Franklin: We did have a moment on day two – Thursday evening – with some of the code we’d written that day. We had assumed it was working correctly, but it was actually broken. That was a pretty low point. We took a step back to review what we’d done and troubleshoot. We ended up writing new code, but even then we felt confident that we could go back to the original code if we couldn’t resolve the issue.

BNR Blog: Which team would you consider your nemesi?

Franklin: That’s the funny part. While it was a competition, all the Nerds were involved in this awesome collective experience. We were doing it together.

We weren’t concerned about winning, we wanted to take this really cool idea to fruition. It was a fun challenge to get it working as quickly as we did.

Okay, maybe we did want to see the grand prize of “conspicuous technology” promised to the winners!

BNR Blog: How did you feel as the clock ticked down to Friday’s deadline?

Franklin: We finished Roger three hours before the final deadline. We had some ideas for more functionality, but three hours just wasn’t enough time to implement any of them.

We have continued to add to it since then though. In case you missed it the first time, you can see it at https://github.com/bignerdranch/Roger.

BNR Blog: Well, congratulations again Team Franklin! Big Nerd Ranch’s Most Bad-Ass Coders. You can view an overview video of Roger.

And, dear reader, if you were surprised that an Android app rose to the top in Clash of the Coders, consider that Big Nerd Ranch is a key player in the Android market. We have a thriving Android consulting business, teach a popular class and will debut our first Android Programming: The Big Nerd Ranch Guide this summer.

Franklin Collage

Our Nerds are fluent in many languages; Dothraki, Klingon, Objective-C, Java, Elvish and Wookiee.

Both kinds of Cocoa

If you’re an iOS programmer who only programs CocoaTouch, I want to encourage you to give desktop Cocoa a try. I’ve primarily been shipping iOS software for the last two years, but I’ve also been doing a fair amount of Cocoa programming on the desktop during that time, either writing helper tools or implementing parts of the iOS app on the desktop.

Program turnaround time is much faster on the desktop. You don’t have to wait for your code to load up on a device or for the simulator start up. You also don’t have to worry about “The simulator is already in use so I’m not gonna run your code right now” smackdowns from Xcode or your provisioning profiles have gone walkabout.

Drawing Models

The drawing models are the same on iOS and on the Mac, and the toolkit support classes are similar even if they’re spelled differently (UIColor and NSColor, UIBezierPath and NSBezierPath). If you’re drawing with CoreGraphics, much of the code is not only similar, it’s identical between the desktop and the device. I have a personal app that supports the desktop and device where all of the model code and all the drawing code is identical, with not an #if or #ifdef in sight. Platform specific code is used to make the containers that holds the drawing and for soliciting configuration information from the user using native UI controls.

Here’s a screen shot of the Class Builder app I built for Cycling Fusion. It has three striped gradient views. There’s one with the five different major settings, and two for the heart rate: zone low/mid/high and one for the five different heart zones. So there are three different slabby things on this screen. The actual drawing is kind of complicated – the different slabs are darker as they go down, but each one has its own gradient from lighter to darker.

Classbuilder slabs

The graphics designer delivered a photoshop file that we used that to generate different pngs for display. But we were moving things around during development and resizing them to get things Just Right as we iterated on the features. Regenerating six different graphics assets (the three you see here in regular and retina sizes) was unfun. I decided to implement it in Core Graphics (a.k.a. Quartz). That way it would draw at maximal resolution, as well as be a component easily reusable in other apps.

Luckily many Core Graphics drawing model features are similar to Photoshop which made implementation straight-forward. I used a Mac application do to the work:



I could iterate very quickly. Implement the rounded corners. Resize the window to make sure things looked good small and large. Then fill in the slabs. Run and resize the window to make sure things stayed sane when the slabs were made larger and smaller. Once I had everything looking how I wanted, it was a piece of cake to put the Quartz code into the iOS project.

Support Tools

Support tools are another place where desktop Cocoa comes in handy. You won’t ship these tools to customers, but you use them to create and edit things that the iOS app uses.

Another side project I have is a music dictionary geared towards musicians in a rehearsal who are wanting to look up terms quickly. Type in part of a term and it’ll filter the list to match what you type in:

Rehearsal dictionary

I wrote a Mac application to edit the dictionary, which is populated (manually) by scouring markings in actual orchestral and band parts. I have tools to help me find duplicates in the data set, quickly attach tags for easier browsing, and generally massage things so that the app can load the database quickly:

Dictionary tool

Being comfortable with Cocoa and CocoaTouch lets me construct model objects, as well as drawing code that can be used in both worlds, making me a more awesomer developer.

Shameless Plugs

And, as you can probably guess, the Big Nerd Ranch can handle all of your Desktop Cocoa training needs! The definitive Cocoa programming book is now in its fourth edition, and we also have an Beginning Cocoa class coming up in August.

Table Scanning

Every now and then I get a question about an idiom I use for looping through a collection of literal structures. This is handy for little lookup tables, or using pre-defined data to populate another data structure.

This is all generic C stuff, so you can use it in Objective-C too. I’ll be walking through part of the fsevent.m example code from AMOSXP(3). It takes a set of bit flags from FSEvents (Mac OS X file system notifications) and prints out, in a human-readable format, which of the bits are set.

Making the Table

First you need a structure to hold the data. In this case, you’ll want to associate human-readable strings with bit-flag constants of the FSEventStreamEventFlags enum. So you’ll need a field with the bitflag, and another pointing to a C string that has the description:

// Lookup table for displaying human-readable translations of flags
typedef struct FlagMap {
    int bitflag;
    const char *description;
} FlagMap;

Then define the lookup table. There’s two interesting things about this table. There is no explicit sized used when declaring the flagmap array. The array will be as big as it is. There is no need to count the elements by hand and put an explicit value in the square brackets. The other is that there is no sentinel value (a zero or NULL / nil) to terminate the table. The table only contains interesting data.

static FlagMap flagmap[] = {
    { kFSEventStreamEventFlagMustScanSubDirs, "must scan subdirs"     },
    { kFSEventStreamEventFlagUserDropped,     "user dropped events"   },
    { kFSEventStreamEventFlagKernelDropped,   "kernel dropped events" },
    { kFSEventStreamEventFlagEventIdsWrapped, "event ids wrapped"     },
    { kFSEventStreamEventFlagHistoryDone,     "history playback done" },
    { kFSEventStreamEventFlagRootChanged,     "root changed"          },
    { kFSEventStreamEventFlagMount,           "mounted"               },
    { kFSEventStreamEventFlagUnmount,         "unmounted"             }
};

Now to the actual use of the table. The FSEvents callback function walks through a set of file system change events and prints out various bits of interesting information. To see what bits are set, walk through the table each time and bitwise-AND the flag to see if it’s set.
That’s the algorithm in words. Here it is in code.

...
        FSEventStreamEventFlags flags = eventFlags[i];

        // Display all of the set flags.
        FlagMap *scan = flagmap;
        FlagMap *stop = scan + sizeof(flagmap) / sizeof(*flagmap);
        while (scan < stop) {
            if (flags & scan->bitflag) {
                printf ("    %s\n", scan->description);
            }
            scan++;
        }
...

Uh. OK. If you’re not familiar with C and C pointers, this may seem like gibberish. The technique is a simple scan through bytes in memory.

This is how things are laid out:

Scanstop 1

For the sake of discussion, I’ll say it’s four bytes for the constant and four bytes for the pointer. So as you scan through memory you’ll see four bytes of a constant, and four bytes for a pointer, then four more bytes of the next constant, and four more bytes for the next pointer, stacked end-to-end.

Walking the Table

So, to start out with, set the scan pointer to the start of table:

FlagMap *scan = flagmap;

Now scan points at the beginning of the table:

Scanstop 2

How do you know when to stop? If you had a sentinel value, you could keep incrementing the scan pointer until you found the “please stop now!” value. But this table doesn’t have that. Sometimes you might not have the luxury of a value that’s out of bounds you can use for a terminator. There’s also no explicit size. To know how many items to look at, you need to do a little bit of math.

The compiler knows how big the flagmap table is : 64 bytes. Four for the bit flag, four for the pointer, and eight entries. You can get this value with sizeof(flagmap).

The compiler also knows how big a single element of the table is. The table is composed of FlagMap structures, which are eight bytes each. You can get this value with sizeof(FlagMap). There’s a trick you can do with sizeof – rather than hard-coding the type, you can get the compiler to figure out what the type is. sizeof(*flagmap) will take the type of the flagmap variable, which is a C Array, which is basically a pointer to the first element of the array, and then dereferences it. The type of this is FlagMap. So, sizeof(*flagmap) will also evaluate by the compiler to 8. I prefer doing it this way, basing the sizeof off of the actual array variable, because you might change the type of the array and would have to remember to update the “size of the structure” code.

Dividing the size of the array by the size of an element gives you the number of items in it: sizeof(flagmap) / sizeof(*flagmap).

So now, the next line of code figures out where the end of the table is:

FlagMap *stop = scan + sizeof(flagmap) / sizeof(*flagmap);

The expression then turns into

FlagMap *stop = scan + 8;

If you remember your C pointer rules, when you add one to a pointer, you’re actually incrementing the value of the pointer by the number of bytes of its base type. In this case scan + 1 is the value of scan, plus eight for the eight bytes of sizeof(FlagMap). scan + 2 actually results in scan plus sixteen, and so on. By adding 8, you’re setting the end pointer to point 64 bytes after the start of the table, which happens to be the end of the table.

Scanstop 3

Technically, it’s pointing right off the end of the table. It does give you an address in memory where you know you’ve run out of table contents.

And now the preparation is over. You know where in memory to start looking for stuff (scan), you know where in memory to stop looking for stuff (stop).

Now for the loop. This is the idiom:

while (scan < stop) {
    // Do stuff with scan
    scan++;
}

So long as the scan pointer is strictly earlier in memory than the stop pointer, do some work with scan. Then increment scan by one. Remember that adding one to the scan pointer actually adds eight to it due to the size of what it points to. The pointer now points to the beginning of the next element of the table. Then you process it. Then increment it again, and point to the next element of the table, and process it again.

Here’s all the values that scan has during the course of processing:

Scanstop 4

Notice that when scan+8 == stop, the loop stops because scan is no longer strictly less than stop.

Actually Doing Stuff

What kind of work can you do? That’ll be defined by what data you have in your table. In this case,

if (flags & scan->bitflag) {
    printf ("    %s\n", scan->description);
}

It takes the bitflag from the table, bitwise-ANDs it to see if it’s set in the flags gotten from the file system event. If the bit is set then the if expression evaluates to a true value, so the body of the if is taken prints the description.

Want to see some more examples of this stuff, check out this gist, which includes walking some simpler tables.

A Motivation for ivar decorations

An idiom used by some Objective-C programmers is prefixing instance variable names with underscores. You do see this with explicitly declared instance variables:

@interface BNRSoapBubble : NSObject {
    float _viscosity;
    NSString *_detergentBrand;
}
@end

Or via properties when you synthesize them:

// soapbubble.h
@property (whatever) float viscosity;
@property (whatever) NSString *detergentBrand;

// soapbubble.m
@synthesize viscosity = _viscosity;
@synthesize detergentBrand = _detergentBrand;

These synthesizes tell the compiler that each property should be backed by an instance variable whose name is prefixed by an underscore. The compiler names the backing instance variable after the property if you don’t provide an alternative.

OK, so why do programmers do this? Seems like it’s just extra busywork. I see two main reasons: one one involves style, and one involves safety.

It’s Not Reserved!

But first – there’s a common misconception, unfortunately promulgated by some book authors and others that Should Know Better, that Apple reserves leading underscore instance variables. That is totally not true. Apple reserves leading underscores on method names, but not instance variables. You can google for “Naming Properties and Data Types site:apple.com” to see the recommendations.

Being stylish

Many style guides, whether just your favorite way of laying out code, or from a corporate mandate, have instance variables (or member variables) decorated somehow to distinguish them from arguments or local variables. I use underscored instance variables to make my setters prettier:

- (void) setPonyName: (NSString *) ponyName {
    [_ponyName autorelease];
    _ponyName = [ponyName copy];
}

This way I can use “ponyName” as the argument, which I find very readable. I also like being able to see at a glance when something is an instance variable or not. One thing which I’m not a fan of is cooking up some kind of abbreviation, which is what many programmers do:

- (void) setPonyName: (NSString *) pn {
    [ponyName autorelease];
    ponyName = [pn copy];
}

I find “pn” hard to mentally pronounce. And I don’t like nomenclature like “myPonyName”, it sounds juvenile, reminiscent of “Fisher-Price My First Programming Lesson.”

Some people use a trailing underscore (that’s Google’s style), and folks also prefix or suffix with “m” for “member”. It’s all fine. One nice thing about the leading underscore is that it automatically participates in Key-Value Coding.

Being Safe

Nowadays, with dot notation, having the backing ivar being spelled the same as the property that it backs can cause subtle and difficult to track down bugs, bugs which are impossible to make if you prefix your instance variables.

huh?

Recall that with dot notation this line of code:

self.ponyName = @"Mikey";

is exactly the same as

[self setPonyName: @"Mikey"];

These two lines of code are identical. self.ponyName causes a method to be called. Code is run. All sorts of stuff happens. With manual memory management, you would be doing retains and releases. Potentially mutable arguments are copied. Subclasses can override setPonyName: and affect the incoming parameter, or do additional, necessary work. It’s pretty open-ended what can happen.

Now this code:

ponyName = @"Mikey";

just copies the four bytes (eight bytes on 64-bit Mac. I’m going to call it four bytes from here on out) of the address of @"Mikey" into the four bytes of the ponyName instance variable. None of your code is called. None of the subclass code is called. The pointer just points to a new object. Nothing else happens.

What if you were really intending to do self.ponyName here to take advantage of the extra work the method does? Because you’re just assigning the naked instance variable, the extra code inside of setPonyName: will not be run. It’s entirely possible that messes up your program state. Perhaps, for legal or regulatory reasons, you need to register all pony name changes. You’re now breakin’ the law, breakin’ the law.

A little more concretely. Consider this implementation:

@property (copy) NSString *name;
...
@synthesize name;
...
- (void) setPonyInfoFromPropertyDict: (NSDictionary *) ponyProperties {
    NSString *name = [ponyProperties objectForKey: @"Name"];
    ponyName = name;
}

The backing instance variable is the same as the property, without an underscore. We have assigned the four bytes of the address of the name to the instance variable. We did not copy it the string. We’ve broken the contract. This is a bug.

How could this bug manifest?

NSMutableString *mutableName =
    [NSMutableString stringWithString: @"Mikey"];
NSDictionary *ponyProperties =
    [NSDictionary dictionaryWithObject: mutableName
                  forKey: @"Name"];
[pony setPonyInfoFromPropertyDict: ponyProperties];
[mutableName setString: @"Wookiee"];

And now, whenever we ask for the pony’s name, it’ll be “Wookiee” instead of “Mikey”. By having a the backing instance variable be underscore-prefixed, the offending line of code:

    ponyName = name;

Would be flagged as an error by the compiler.

Trust me: at 3 in the morning across the country right before a trade show, hyped up on Dr Pepper and Slim Jims, it can be very hard to not read “self.blah” for “blah” if that’s what you’re expecting/hoping the code would do.

From The Real World

OK, we’re not all lucky enough to be developing a pony management system. How about a more concrete example that actually happened in real life? Some friends in the local CocoaHeads asked me to help debug a weird problem with their iPhone app. They’d tap a button on a screen which drew an image (amongst other stuff). Then they’d go back to their program’s main menu screen and come back to this page, and be greeted by a crash inside of UIImage.

The image page had a view with an UIImage property (which was pre-ARC):

@property (nonatomic, retain) UIImage *mapImage;

They got the map image from UIImage, and set the property:

mapImage = [UIImage imageNamed: @"Map"];

And then they’d do stuff with the image, like draw it in drawRect:

- (void) drawRect: (CGRect) rect {
    ...
    [mapImage drawInRect: someOtherRect];
    ...
} // drawRect

It drew fine, and but when they moved to a different part of the app and came back, it would crash.

Did you find the bug? I didn’t find it the first time I looked at the code. Go back and take a look.

I ended up doing an impromptu demo of Instruments retain count tracking to isolate it. Only when I noticed “That’s odd. It’s not being retained here” did I find it. It’s this:

mapImage = [UIImage imageNamed: @"Map"];

This says “Hey UIImage, get me an image called Map”. UIImage says “ok, here is the address of an image from my cache. Enjoy.”. The four bytes of that image address are copied to the four bytes of mapImage.

Notice the image is not retained. It was not manually retained here. It did not go through the setter method that would have retained it. That’s the bug.

What then happened is the user went to the other screen. It triggered a memory warning. UIImage reacted to the memory warning by to releasing all of its images. Because the map view didn’t retain the image, that particular image’s retain count went to zero, and the image was destroyed. My friend still had a pointer to it, which is now a dangling reference, and it crashed the next time it tried to draw the image.

They really wanted to do

self.mapImage = [UIImage imageNamed: @"Map"];

which causes setMapImage: to be called, which would have retained the object.

How would the underscore prefix fix this? With underscore prefixed instance variables, these lines of code compile and work:

UIImage *map = [UIImage imageNamed: @"Map"];
self.mapImage = map;
_mapImage = map;

and it’s obvious what’s a self-send, and what’s an ivar assignment. “Oh. Underscore. ivar assignment.”

You would get a compiler error, not just a warning but a stop-you-in-your-tracks error, with the morally ambiguous code:

mapImage = map;

The error happens because the compiler doesn’t know what mapImage is. It’s not an instance variable (that’s _mapImage). It’s not a property access (that’s self.mapImage), therefore it’s an error.

What About ARC?

So what about ARC? Instance variable pointers are strong references, so the original case of using instance variables named after the property would have retained the map:

mapImage = map;

It would therefore not be accidentally destroyed, and the program wouldn’t crash. Problem Solved! No need for those pesky underscores and the their corresponding confusion and religious debates.

What if the original intent of that code was to vector through the setter method? The semantics of the assignment is now incorrect. Harkening back to the Pony Regulatory Agency, it’s possible to have code that works (or at least doesn’t crash and the screen seems to display the correct stuff) but is still incorrect because necessary support work isn’t being done. ARC has now masked this problem, turning an obvious crasher with manual memory management into a silent bug.

self.done = YES;

Readability reasons aside (which are purely stylistic), the safety afforded by the different instance variable and property names when using dot notation for setters is why I prefix my ivars with underscores. Everything in Advanced Mac OS X Programming: The Big Nerd Ranch Guide adopts this pattern as well (there’s a brief discussion at the bottom of page 38 of the dead-trees version). It’s a pretty easy way to prevent this particular class of bugs. In the course of teaching various courses, and talking with folks at conferences, many programmers are still fuzzy over the differences between property-method-calling and just assigning a pointer. Adopting this underscore-prefix technique is a rote way of avoiding a class of problems until the concepts have had time to sink in.

Protocols Part 3 : Adopting Protocols in Class Extensions

One of the directions Apple is taking in Objective C that I’ve come to really like is the migration of stuff out of header files. I’m a firm believer that header files should only contain the public programming interface, along with any bookkeeping the compiler absolutely has to have And nothing else. Anything that doesn’t contribute to a person’s understanding of how to use your class shouldn’t be in there.

When I first started programming iOS I didn’t like the explicit protocols for data sources and delegates. I could understand why Apple does it, to make it explicit that “Hey! I know what I’m doing when I say I’m going to be a data source.” Things are much more explicit than what you get with an informal protocol, which is just a category. But it made the header feel kind of junky.

Say that one of my teammates on the BigLunch team wrote a view controller that lets the user choose a sandwich. It has a table view for picking the sandwich type (grinder, sub, hero, torpedo) and a spinner-picker for sandwich fillings (tofurkey, spam). The header file would look like this:

// BNRSandwichChooser.h
@interface BNRSandwichChooser : UIViewController <UITableViewDataSource,
                                                  UITableViewDelegate,
                                                  UIPickerViewDataSource,
                                                  UIPickerViewDelegate>
- (BNRSandwich *) chooseLunch;

@end // BNRSandwichChooser

The table view and picker control are just implementation details. I, as a programming using this view controller, don’t care that it uses these particular UI objects. I just want to let the user choose lunch, and then go and do other stuff with their lunch choice. But, the BNRSandwichChooser needs to adopt the protocols somewhere so that the compiler won’t complain when setting the delegate:

- (void) viewDidLoad {
    self.tableView.datasource = self;
} // viewDidLoad

If the lunch chooser implementation changes to a text field or a web view, I don’t want my code to have to be recompiled because the header changed.

When Apple added class extensions, that bit of syntax that looks like a nameless category declaration, they also added the ability to to conform to protocols there:

// BNRSandwichChooser.m
#import "BNRSandwichChooser.h"

@interface BNRSandwichChooser () <UITableViewDatasource,
                                  UIPickerViewDataSource, ...>
@end

@implementation BNRSandwichChooser
...
@end

Leaving the interface nice and minimal:

@interface BNRSandwichChooser : UIViewController
- (BNRSandwich *) chooseLunch;
@end

Fixing the Map

If you remember last time, I showed a world map that used a delegate to get information from another class, and also to inform it that stuff happened. Here is what the header looked like:

#import <Cocoa/Cocoa.h>

#import "BNRWorldMapView.h"

@interface BNRAppController : NSObject <NSApplicationDelegate,
                                        BNRWorldMapViewDelegate>
@property (unsafe_unretained) IBOutlet NSWindow *window;
@property (weak) IBOutlet BNRWorldMapView *worldMap;
@end // BNRAppController

It is kind of junky, having to include the map header (forcing a recompile of BNRAppController if you change a comment in BNRWorldMapView.h), and adopt the protocol. To clean up the header, remove the import and the adoption, and add a @class to forward-declare the world map view so Interface Builder will know what type to target a connection to;

// BNRAppController.h
#import <Cocoa/Cocoa.h>

@class BNRWorldMapView;

@interface BNRAppController : NSObject <NSApplicationDelegate>
@property (unsafe_unretained) IBOutlet NSWindow *window;
@property (weak) IBOutlet BNRWorldMapView *worldMap;
@end // BNRAppController

And then in the implementation, I’d add the adoption to the class extension:

// BNRAppController.m
@interface BNRAppController () <BNRWorldMapViewDelegate>
@property  NSMutableSet *selectedCountries;
@end // BNRAppController

The takeaway? Unless the protocol adoption has to be public for other classes to compile correctly, I now put all implementation-detail protocol adoptions into a class extension in the implementation file. This keeps the implementation details nice and hidden, and keeps the interface clean.

Protocols part 2 : Delegation

Last time we talked about protocols and why you’d want to use one. So, when would you want to make your own protocol? You make protocols when you’re defining some kind of mechanism for your object to use other objects to do its work. One use of protocols is defining the set of methods used for plugins. AMOSXP(3) has a section that builds plugins in Cocoa, using a protocol to spec out how the plugin and the host application interact with each other.

Another use for making your own protocol is to set up a delegate relationship between two objects. That’s what I’ll show here.

The class providing the delegate

The example for this posting is making a world map view. You can pick up the code from here. The code is extensively commented, and covers details (like how the map was created) that aren’t covered here.

After an extensive design session these features were deemed necessary for the map:

  • Draw the countries of the world
  • Draw each country in an arbitrary color
  • Let the user click on each country and have something happen

I expect the map will be in a window, and there will be a controller object (an App Controller, or a View Controller) that manages it.

There’s a number of different ways you can architect the map view to satisfy the feature list. One way is to have the controlling object supply an array or dictionary of colors to the view, and use target/action to handle clicks.

The way I’m going to do it here is to use a delegate object. The App Controller will be the object used as the delegate. The map will ask its delegate what color each country should be. The map will inform its delegate when a country is clicked on.

Here’s screenshot of the program. You’ll see why Finland is in red a little later. I clicked on the orange countries.

World map

The Map View Header

Time for the header. Here is the complete interface for the map view:

#import <Cocoa/Cocoa.h>

@protocol BNRWorldMapViewDelegate;

@interface BNRWorldMapView : NSView
@property (nonatomic, weak) id <BNRWorldMapViewDelegate> delegate;
@end // BNRWorldMapView

@protocol BNRWorldMapViewDelegate <NSObject>

- (NSColor *) worldMap: (BNRWorldMapView *) map
              colorForCountryCode: (NSString *) code;

@optional
- (void) worldMap: (BNRWorldMapView *) map
         selectedCountryCode: (NSString *) code;

@end // BNRWorldMapViewDelegate

Whoa. There’s actually a lot going on there. Let’s take it apart, piece by piece.

#import <Cocoa/Cocoa.h>

Thus pulls in the necessary interfaces for Cocoa.

@protocol BNRWorldMapViewDelegate;

I first forward-declare the protocol. It’s just promising “there is a protocol by the name BNRWorldMapViewDelegate” so that I can declare a property that references it, but I haven’t told anyone about the details yet. I could have put the whole protocol definition up here, but I like putting it later in the header file. This lets other programmers read the object’s overall programming interface before getting down into the details of the delegate or datasource protocols. You could list the protocol first, but you would need to forward-declare the map view class with @class, which can be just as confusing. I always do it this way.

@interface BNRWorldMapView : NSView

No surprises here. Just a new NSView.

@property (weak) id <BNRWorldMapViewDelegate> delegate;

This declares the map’s delegate property. It is an id, meaning that any Objective-C object, no matter its parentage, can participate. The id adopts the map view delegate protocol to force any object that is assigned to this property to declare that it conforms to that protocol. People setting their objects to be the delegate would assign their object pointers to this property, e.g. map.delegate = self; More on this in a little bit.

Notice that this property is declared weak. If you’re not using ARC, you would use assign. This is a standard technique for breaking retain cycles.

@end // BNRWorldMapView

All done with the view’s programming interface. Now for the protocol.

@protocol BNRWorldMapViewDelegate <NSObject>

This starts the protocol declaration. Protocols can adopt other protocols, and the map view delegate protocol adopts the NSObject protocol. It’s OK to be kind of confused about this. The term NSObject is used for both a class (e.g. NSAffineTransform inherits from NSObject), and for a protocol (e.g. NSTableViewDataSource adopts NSObject, this map view delegate protocol adopts NSObject). Why am I adopting NSObject? Mainly so I can call respondsToSelector: without complaint from the compiler.

- (UIColor *) worldMap: (BNRWorldMapView *) map
                   colorForCountryCode: (NSString *) code;


This is the declaration of the method that the controller classes would need to implement. Say you had a view controller whose view contains a map, and has a pointer to the map via an IBOutlet. That view controller would set itself to be the map’s delegate, and it must implement this method to supply the color for the given country code. This is a required method. Protocol methods are required by default, unless made optional.

The pattern you should use for protocol methods is to make the map object the first argument. If you’re not making maps, then the type of the first argument would be your class that has the delegate property. Inside of the world map view implementation it would call the method and pass self as the first argument. This is done so that one object can be the delegate for multiple maps. The first argument is used to disambiguate (I love that word!) between different map views.

@optional
- (void) worldMap: (BNRWorldMapView *) map
         selectedCountryCode: (NSString *) code;

This is an optional method, letting the user click on a country. The map informs the delegate that a country was selected. It’s up to the delegate to figure out what the right thing to do is. This is an optional method, so the object that’s going to become the map’s delegate doesn’t need to implement it. That’s why I’ll need to call respondsToSelector: first, and that’s why the BNRWorldMapViewColorDelegate protocol needs to also adopt the NSObject protocol.

@end // BNRWorldMapViewColorDelegate

And we’re done with the protocol declaration.

The Map View Implementation

Now to see how it’s used inside of the map view. The map view overrides drawRect: so that it can draw each of the countries. g_countryPaths is a global NSDictionary that maps country codes to NSBezierPath objects:

- (void) drawRect: (CGRect) rect {
    [[NSColor darkGrayColor] setStroke];

    for (NSString *countryCode in g_countryPaths) {
        NSBezierPath *path = [g_countryPaths objectForKey: countryCode];

        // Ask the delegate.
        NSColor *fillColor = [self.delegate worldMap: self
                                  colorForCountryCode: countryCode];
        if (fillColor == nil) fillColor = [NSColor whiteColor];

        [fillColor setFill];
        [path fill];

        [path stroke];
    }

} // drawRect

drawRect: loops through the collection of countryCodes, pre-populated by the map view. For each countryCode, it fetches an outline for the country so it knows what area of the map to fill.

Now the important part:

NSColor *fillColor = [self.delegate worldMap: self
                          colorForCountryCode: countryCode];

This asks the delegate for the country code color. You can see it passing self for the first argument, and then the country code in the second argument. This is one of those techniques that makes Objective-C so much fun. The map code doesn’t care what kind of object the delegate is. It could be a view controller. It might be a table view cell. It might be some model object. It might be a distributed object that actually lives on another computer. The map code really doesn’t care, just so long as it can respond reasonably to worldMap:colorForCountryCode:, and the existence of the protocol ensures that happens or else the compiler will complain.

What about the optional method? It’s pretty easy:

- (void) mouseDown: (NSEvent *) event {
    NSPoint mouse = [self convertPoint: event.locationInWindow
                          fromView: nil];

    for (NSString *countryCode in g_countryPaths) {
        NSBezierPath *path = [g_countryPaths objectForKey: countryCode];

        if ([path containsPoint: mouse]) {
            NSLog (@"clicked in %@", countryCode);
            if ([self.delegate respondsToSelector:
                          @selector(worldMap:selectedCountryCode:)]) {
                [self.delegate worldMap: self
                               selectedCountryCode: countryCode];
            }
        }
    }

} // mouseDown

Here it loops through the country codes until the point lies in a country. Then it checks to make sure the delegate responds to the optional selector. If it does, tell the delegate the good news.

Quick aside – I could have checked that the delegate responds to the selector at the very beginning and completely bypassed the work of the method by using an early return. For purposes of illustration I’m assuming there’s some extra work that would happen before or after the delegate gets notified.

OK, now done with the map view! It defines a delegate, and the map view uses that delegate to figure out how to draw each country, and to tell someone else that a country was tapped on.

The class using the delegate

So, there’s now a map view that has delegation. How do you use it? Pretty easy. This program is simple, with the BNRAppController class acting as the controller. The Xcode templates would have called this BNRAppDelegate, but I thought that having too many “Delegate” names floating around would be too confusing, so I renamed it to be BNRAppController.

The AppController’s Header

Here’s the header for BNRAppController:

#import <Cocoa/Cocoa.h>

#import "BNRWorldMapView.h"

@interface BNRAppController : NSObject <NSApplicationDelegate,
                                        BNRWorldMapViewDelegate>
@property (unsafe_unretained) IBOutlet NSWindow *window;
@property (weak) IBOutlet BNRWorldMapView *worldMap;
@end // BNRAppController

This is pretty straightforward, mostly template boilerplate. I did have to pull in the map view’s header so that the compiler can see the delegate definition, and I adopt the BNRWorldMapViewDelegate in the class interface. It’s also possible to adopt the protocol in a class extension. I’ll talk about that next time.

The AppController’s Implementation

One of the things the app controller does is keep track of what countries were clicked on so. Clicking a country toggles the color, so it has to keep track of what the “currently selected” countries are. A mutable set is perfect for holding on to the country codes of the selected countries:

@interface BNRAppController ()
@property  NSMutableSet *selectedCountries;
@end // BNRAppController

Now that the bookkeeping is out of the way, you would set the map’s delegate pointer in awakeFromNib:, and also create the set that holds on to the selected countries.

- (void) awakeFromNib {
    self.worldMap.delegate = self;
    self.selectedCountries = [NSMutableSet set];
} // awakeFromNib

Now implement the colorForCountryCode method. What colors should I use? It turns out that Finland has the highest per-capita density of heavy metal bands, so they get a nice bright red color for their awesomeness, while everyone else gets a mellow yellow. Selected countries get colored orange.

- (NSColor *) worldMap: (BNRWorldMapView *) map
              colorForCountryCode: (NSString *) code {

    if ([code isEqualToString: @"FI"]) {
        return [NSColor redColor];
    } else {

        if ([self.selectedCountries member: code]) {
            return [NSColor orangeColor];

        } else {
            return [NSColor yellowColor];
        }
    }
} // colorForCountryCode

So now when the BNRWorldMapView wants know the color of Finland, this code gets run, and fills Finland in with red. It calls this method for all of the other countries, and gets yellow or orange colors.

The final piece of code is the optional "hey the user clicked on a country" method. It gets passed the pointer to the map that's calling the method, and the country code:

- (void) worldMap: (BNRWorldMapView *) map
              selectedCountryCode: (NSString *) code {
    if ([self.selectedCountries member: code]) {
        [self.selectedCountries removeObject: code];
    } else {
        [self.selectedCountries addObject: code];
    }

    [self.worldMap setNeedsDisplay: YES];

} // selectedCountryCode

This is just a little work to add or remove countries from the set (a click toggles), and schedules a redraw for the map.

And that's it! We now have an object that uses a delegate (the map view), and another object that acts as a delegate (the app controller).

What direction?

A question came up during an Advanced iOS Class where a student was unclear about which class defines the protocol and which one uses it.

Here's the pattern: the class which is using another object, whether to get data from it like a UITableDataSource, or if it's needing to notify another object like a UITextViewDelegate, is the class which defines the protocol and has the id <protocolname> property. It knows what data it needs, and it knows what actions it needs to tell others about, so it's the one that's best qualified to define the protcol.

The classes which are being used, the ones that answer the questions "Hey, you need such-and-such information? The value to use is 'Schmoo'", or "Oh, such-and-such happened? That's cool! I'll change the icon in my tab bar." are the ones that implement the methods of the protocol, and set themselves to be the delegate of the other classes. Hopefully a little diagram helps:

Delegate illustration

Wrap up

Delegation is a very powerful pattern, allowing cooperating objects to decouple themselves from each other, while still cooperating.

Special thanks to Al MacDonald, who created the world map and posted it to Wikipedia: http://en.wikipedia.org/wiki/File:World_map_-_low_resolution.svg. Also thanks to Martin Haywood who created a SVG-to-BezierPath utility. These two useful chunks of work let this posting have real code.

Protocols, part 1 : Why?

A comment from a reader of Objective-C Programming: The Big Nerd Ranch Guide came through on the forums, asking about Objective-C protocols. Specifically, why do we need them? Why do they exist?

As a reminder, a protocol is a list of methods that’s been given a name. The NSCopying protocol contains a single method copyWithZone:, while the NSCoding protocol contains encodeWithCoder: and initWithCoder:. You adopt a protocol by adding the protocol name in angled brackets after the class you’re inheriting from:

@interface BNRGroovyness : NSObject <NSCoding>

This @interface says that “BNRGroovyness inherits (is-a) NSObject, and so can be used anywhere an NSObject can be used. And also, it promises to implement the required methods in the NSCoding protocol.” The compiler will check your work to make sure you actually implement them. Of course, you don’t have to implement optional methods in the protocol unless you really want to.

Like many programming constructs, protocols are primarily a communication mechanism. They communicate to the compiler that these objects really can receive a particular set of messages. They also communicate to the reader of the code that this class has some higher-level semantics. “This class conforms to NSCoding, therefore I can encode and decode its objects into a data archive” or “Oh! This class conforms to UITableViewDataSource! I can use its objects to power a table view.” Xcode can also use protocol adoption to expand the list of autocomplete options, making it easier to correctly spell the names of the methods you’d be implementing.

Protocols allow for a kind of multiple inheritance in Objective-C. You don’t really inherit code from multiple classes, but you can inherit multiple contracts, stating that you’ll obey those contracts. If you’ve used Java, they’re just like Java interfaces, which interestingly enough were inspired by Objective-C protocols. So when you’re adopting multiple protocols, all the code is implemented in your class or comes via the single-inheritance chain.

Protocols come into their own when you’re handing an object to another object for it to do work. If you’re giving an object to an NSKeyedArchiver, that object needs to support NSCoding so that the archiver can serialize the object. If you’re giving an object to a UITableView, it needs to support UITableViewDataSource so that it can feed the table. This helps move some of the correctness tests from run-time (“oops, this object doesn’t implement -encodeWithCoder: . Time to crash!”) to compile time ("warning: sending 'BNRGroovyness *' to parameter of incompatible type: id<NSCoding>")

One very popular use for making your own protocols is setting up a delegate relationship between objects. Come back next time for details.