Archive for March, 2008

NSLog an NSString

NSLog(@"hello, %@", @"world");

mercurial error ‘ValueError: unknown locale: UTF-8′ on MacOS 10.5

Having installed mercurial via macports, any hg command fails with:

   <nasty Traceback snipped>
   ValueError: unknown locale: UTF-8

This is mercurial 0.9.5 and Python 2.5.1, but its seem that its caused by Leopard’s Term.app not setting the local encoding correctly. To resolve this, add the following to .profile:

   export LC_ALL=en_US.UTF-8
   export LANG=en_US.UTF-8

as noted in the mercurial mailing list.

classpath file classloader – now with properties

The ClasspathFileClassLoader now supports system properties. Thus, the command line for any Java application need consist of only:

   java \\
   -Djava.system.class.loader=tierseven.cfcl.ClasspathFileClassLoader \\
   -Dtierseven.cfcl.ClasspathFileClassLoader.properties_file=/path/to/cfcl/properties_file \\
   -classpath /path/to/cfcl/classes \\
   <main_class>

The properties file should contain other properties, including tierseven.cfcl.ClasspathFileClassLoader.file, which will in turn contain the classpath elements.

The source is available from a Mercurial repository:

   hg clone http://bitbucket.org/sinistral/cfcl

installing SVK via macports

When installing SVK (2.0.2) via MacPorts (port install svk) make sure that CPAN is installed and has been configured, otherwise the SVK install will go into an infinite loop wating for user input.

This is evident with a port install -v svk. Without the -v it will simply appear to hang at the Configuring svk step); see also MacPorts bug #12050. The problem is that because CPAN has not yet been configured, it is prompting for the configuration that it needs in order to install SVK.

The first time cpan is run from the command line, it will prompt for configuration data; the process can be re-run using o conf init from within the cpan shell, and individual variables can be set with o conf <variable> <value>, e.g.:

   cpan> o conf wget /opt/local/bin/wget

to set, and to unset:

   cpan> o conf wget ""

syntax error before ‘AT_NAME’ token

I also ran into this rather less-than-obvious error message compiling Objective-C code on MacOS X 10.5:

   syntax error before ‘AT_NAME’ token

I eventually resolved it thanks to this blog entry. The compiler was moaning about a missing @end in a header file.

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.