Forge Code

Compile Time Checking of KVC Keys

KVC (Key Value Coding) is a pretty great technology. It’s used throughout the Cocoa APIs, and you might not even realise you’re using it. One of the more common tasks which makes use of KVC is sorting with NSSortDescriptor objects. Here’s a quick example snippet:

@interface Person : NSObject
@property () NSUInteger age;


NSArray *people;  // Contains Person objects
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES]
NSArray *peopleSortedByAge = [people sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

Migrating to Octopress

After putting it off for over 2 years, I’ve migrated my blog from WordPress to something statically generated. Like many developers working with iOS and Mac OS X, I’ve decided to jump on the Octopress bandwagon.

After a few minor teething issues getting RVM configured on Mac OS X 10.7, and quite a few problems getting comments migrated from WordPress to Disqus (third time’s the charm!), I finally got the migration completed this weekend. Now that I don’t have to deal with WordPress (which was giving me the same feeling of unease that I get when I have to use Windows) hopefully you’ll start seeing a few more posts from me!

The Modern Runtime and Clang 1.6’s Shadowed Variables Bug

If you’re writing code in Objective-C, and haven’t heard about the ‘@synthesize by Default’ (and to a lesser extent, ‘Ivars in Class Extensions’), you owe it to yourself to go and read up about it. Martin Pilkington describes the advantages of these new compiler/runtime features in his post New Objective-C Features. Personally, I’ve found ‘@synthesize by Default’ to be a fantastic aid to my productivity, as it means you can create or modify Obj-C properties in only one place (instead of having to keep your @property and @synthesize statements in sync).

UIFont Name Grabber

It’s been a long time between posts here on Forge Code. Since my last post, I’ve made the transition to become a full time freelance iPhone/iPad developer, which, alongside various personal and family events, has meant that my time has been at a premium. This doesn’t mean that I don’t have anything to write about - in fact, quite the opposite. I now have a bunch of topics and snippets queued up in the back of my brain, it’s just a matter of finding the time to get them out into the aether.

Now, onto the actual post…

Creating UIFont objects

The most common method of creating or representing fonts in code in iOS is to use +[UIFont fontWithName:size:]. It’s very straightforward to use: to get a UIFont instance that represents Helvetica 12 point, you would do this:

UIFont *font = [UIFont fontWithName:@"Helvetica" size:12.0];

Using RubyCocoa to Alphabetically Sort Property List Files

When you need small sets of static, structured data for your Mac or iPhone projects, the most common approach is to bundle a Property List (or plist) file in your app. Any object-graphs made solely of ‘property-list objects’ - i.e. instances of NSArray, NSDictionary, NSString, NSData, NSDate or NSNumber - can be easily converted to or from serialised data formats, which means that they can be trivially read from or written to files or network sockets. For more information on plists, see Apple’s Property List Programming Guide.

Simplifying NSNumberFormatter Use on the iPhone With Objective-C Categories

Something that comes up regularly in iPhone development is the need to convert NSNumber objects to NSString objects and vice versa. For converting an NSNumber to an NSString, there is the -stringValue method, which is useful if you want just the plain number without any extra formatting. This isn’t particularly useful for presenting numbers in a user interface however, since -stringValue doesn’t give you any control over how the number is formatted.

A WordPress Plugin for the Mint Bird Feeder Pepper (Mint-bird-feeder)

If you use Shaun Inman’s Mint stat tracking software on a WordPress based site, as I do on Forge Code, you probably use the Bird Feeder Pepper. This ‘Pepper’ - the name given to Mint plugins/widgets - tracks the number of hits on your RSS feeds. The normal way to get Bird Feeder to collect this data is to manually modify the content of the PHP template file(s) in WordPress that are used to generate the content of your RSS feeds.

Making Xcode Work for You When Developing PrefPanes

The first few times I tried developing a Preference Pane in OS X, I had more than a few head-scratching, “what the heck do I do now?” kind of moments. Unlike a typical Mac OS X or iPhone project in Xcode, when you create a Preference Pane project from the built-in Xcode template, doing a ‘Build and Run’ doesn’t result in your project being run. In fact, as of Xcode 3.2.1 the ‘Build and Run’ item in the ‘Build’ menu is disabled ‘out of the box’. This is due to the fact that your project isn’t building an executable program, it’s building a .prefPane bundle/plug-in.

Getting ‘Build and Run’ (⌘+R) to work as you might expect for a PrefPane project (by building your .prefPane, then installing it in System and loading it) isn’t hard, but it’s not at all well documented.

Scripting iTunes With Ruby, and Snow Leopard’s Apple Events Bug

About 2 weeks ago, I made a post to the rb-appscript-discuss mailing list about an error I was receiving while working with rb-appscript. rb-appscript is an event bridge that allows you to use Ruby instead of AppleScript to communicate with scriptable applications on Mac OS X.

As a programmer, I find AppleScript tedious at best. For better or worse, my brain has adapted to understanding programming languages that have non-ambiguous, concise grammars. I find it a lot easier to use a “real” programming language, and luckily enough, in 2009, we can use a number of languages as an AppleScript substitute, including Ruby.