Cocoa is Awesome.

February 8, 2009 | Leave a Comment

I’m just getting started with Cocoa, and I’m having difficulty believing just how easy it is to do useful stuff. Today I wanted to open a dialog box to add a folder path to an object in a Core Data store. “Sigh”, I thought, “This will be a pain.” Fast forward just 10 minutes, and I’d implemented the basics of what I wanted:

NSOpenPanel* openDlg = [NSOpenPanel openPanel];
if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
{
  NSArray *folderToAdd = [openDlg filenames];
  NSString *folderToAddPath = [folderToAdd objectAtIndex:0];
  NSManagedObject *projectToAdd = [[NSManagedObject new] initWithEntity:projectEntity insertIntoManagedObjectContext:context];
  [projectToAdd setValue:folderToAddPath forKey:@"FolderPath"];
  [context save:NULL];
}

“Hmm. That’s cool. But I want the user to only be able to select directories and disable multiple selection.” A quick look at the documentation for NSOpenPanel yielded:

[openDlg setAllowsMultipleSelection:NO];
[openDlg setCanChooseFiles:NO];
[openDlg setCanChooseDirectories:YES];

“Er… Wow. That was seriously easy”, said I, “But surely getting the selected folder name will be tricky?” Wrong. A little more research and two lines of code later, I’d done it.

NSArray *folderToAddPathArray = [folderToAddPath componentsSeparatedByString:@"/"];
NSString *folderToAddName = [folderToAddPathArray lastObject];

It seems that every feature I want to add, provided it’s commonly used, is ridiculously easy to implement. I’ve only been using Cocoa for a matter of days – I wouldn’t even describe myself as a amateur. And yet I can implement features that I thought might take me ages in the blink of an eye.

Cocoa is incredibly well designed and brings Apple’s “It just works” philosophy to the programming world. As I’ve already said, it’s very, very early days for me as a programmer, and I’m sure there will be frustrating times up ahead, but so far I’m very happy. Almost too happy… (cue haunting music).

One problem with this extraordinary ease of use is that I feel like I’m continually cheating. I’ve not come from a traditional programming background, but I have caught a glimpse of programming in other, shall we say, less friendly environments (*cough* Java). I’ve come from the Microsoft world – I once spent 14 hours trying to figure out how to set up a linked server to read an Exchange Server database in SQL Server.

So the idea of creating a new database entry that appears in a table view in 2 lines of code is a real culture shock, and if I’m truthful, makes me feel like a bit of a freeloader. After all, some programmers out there are having to write lots of custom code just to connect to a database and deal with third-party frameworks to do anything useful. And here I am, with only half a clue and no previous exposure to object-oriented languages cobbling together what I need in 10 lines. It doesn’t seem fair somehow.

This may well mean that when things get a lot more involved, I’m out of my depth. This code is not production by any stretch of the imagination. I’m sure that the time will come where the things I want to implement are much more challenging. For example, I have yet to experience the joys of threaded programming, but I’ll cross those bridges asynchronously when I come to them. Whilst maintaining a stringent locking strategy, of course.

For now, I’m just going to enjoy using a tool that seems to deliver on the promise “Common things are easy, and uncommon things are possible”.

Thanks, Apple.

2qw5ypvezx

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon

Comments

Leave a Comment