being objective

I’ve long been a fan of Apple. In 1985 my father returned from a US visit with a brand new Macintosh Plus and introduced me to a whole new world of personal computing (You have no idea how big a deal this was to a little South Africa boytjie – thanks, Dad!). While my friends were fighting with phix so that they could play Montezuma’s Revenge, I had the Dark Castle games. In later years, however, limited access to (relatively) expensive Apple hardware meant that, by the time I started started programming, all that was available to me was Windows and GNU/Linux on Intel hardware.

It is only recently that I’ve had the privilege of getting re-acquainted with the beauty that is Apple.

I’ve been curious about Apple’s choice of Objective-C and, now that I have the opportunity, I’ve decided to learn the language for myself. The bits and bytes that I pick up, I will post here – mostly as a reminder for myself, but also in the hope that it will help someone else who, like me, is just starting out. As most of my experience to-date has been as a Java developer, some of the things that took me a little while to figure out will no doubt be blindingly obvious to a C/C++ developer.

So … my first step was to try to write something very simple that would let me play around with the classes in the standard frameworks. For me this means being able to do a quick command-line build, without having to fire up Xcode. What I ended up with is the following main.m:

#import "Foundation/Foundation.h"

int main (int argc, char** argv)
{
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
	NSMutableArray* records = [NSMutableArray arrayWithCapacity: 3];

	[records addObject: [NSMutableDictionary dictionaryWithCapacity: 2]];
	[records addObject: [NSMutableDictionary dictionaryWithCapacity: 2]];

	[[records objectAtIndex: 0] setValue: @"marc" forKey: @"name"];
	[[records objectAtIndex: 0] setValue: @"male" forKey: @"sex"];
	[[records objectAtIndex: 1] setValue: @"michelle" forKey: @"name"];
	[[records objectAtIndex: 1] setValue: @"female" forKey: @"sex"];

	[records writeToFile: @"entries" atomically: NO];
	[pool release];
}

This may be compiled using:

   gcc -framework Foundation Test.m

There were a couple of things that weren’t immediately obvious to me:

  • to use the classes in the Foundation framework, one needs to include Foundation/Foundation.h; I was looking for specifc headers files for each of the referenced classes.
  • the linker must be instructed to link against the framework using the
       -f <framework>
    option. If you’re not linking, a
       gcc -c main.m
    is sufficient.

0 Responses to “being objective”


  1. No Comments

Leave a Reply

You must login to post a comment.