The latest version of the YAML Cocoa parser is available from the Syck github github.

There’s also a Belorussian translation here – http://www.designcontest.com/show/yaml-cocoa-be

What is it?

After looking at and trying to edit Cocoa’s XML output files or plists I decided there must be a better solution. …and there is. YAML. YAML, in general, is far more readable and consise than XML is. It is used extensively in scripting languages like Ruby. Here are two examples:

YAML (Look Ma’ no markup):

lang=yaml

Author : William Shakespeare
Lines :
– It is a tale told by an idiot,
– Full of sound and fury, signifying nothing.
Birthdate : 1564

And the XML version:

lang=xml


Author
William Shakespeare
Lines

It is a tale told by an idiot,
Full of sound and fury, signifying nothing.

Birthdate
1564

Now you tell me which is more readable.

How to use it

Wouldn’t it be great to use this in your Cocoa apps? Well now you can. Here’s how you do it:

lang=objc
id data = yaml_parse(yamlString);

And that’s it. You can repeatedly call getDocument: to get the remaining documents in the file. At the moment all YAML scalars are read in as strings, which works happily because your code still simply calls floatValue: or intValue: .You can save documents to YAML like this:

lang=objc
[data yamlDescription];

The really neat bits

The stuff you can’t do in xml. Anchors and references. In YAML you can do something like this:

lang=yaml

hr:
– Mark McGwire
# Following node labeled SS
– &SS Sammy Sosa
rbi:
– *SS # Subsequent occurance
– Ken Griffey

and… you can define your own tags real easy. These combined make YAML really powerful. Tags are defined like this:

lang=yaml
– !!NSColor {r: 1, g: 0.5, b: 0}

And here’s the ObjC implementation:

lang=objc
@implementation NSColor (YAMLCocoaAdditions)

+(id) objectWithYAML:(id)data
{
float r=0, g=0, b=0, a=0;

if([data isKindOfClass:[NSDictionary class]])
{
if([data objectForKey:@”r”]) r = [[data objectForKey:@”r”] floatValue];
if([data objectForKey:@”g”]) g = [[data objectForKey:@”g”] floatValue];
if([data objectForKey:@”b”]) b = [[data objectForKey:@”b”] floatValue];
if([data objectForKey:@”a”]) a = [[data objectForKey:@”a”] floatValue];
}

return [NSColor colorWithDeviceRed:r green:g blue:b alpha:a];
}

-(id) toYAML
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:[self redComponent]], @”r”,
[NSNumber numberWithFloat:[self greenComponent]], @”g”,
[NSNumber numberWithFloat:[self blueComponent]], @”b”,
[NSNumber numberWithFloat:[self alphaComponent]], @”a”, NULL];
}

@end

Now start those cogs turning

Think what you could do with all this Power!

lang=yaml

– !!MyCircle
center: &ORIGIN {x: 73, y: 129}
radius: 7
– !!MyLine
start: *ORIGIN
finish: { x: 89, y: 102 }
– !!MyLable
color: !cocoa/NSColor {r: 1, g: 0.9, b: 0.8, a: 0.2}
value: Pretty vector drawing.
start: *ORIGIN

Read it in and you will have an array of three graphical objects all linked to a particular point. The code for these classes needs to be implemented, and of course in proper tradition that is left as an exercise. But this shows how easy it is to use YAML for readable and yet complex data files.

OK, Let me at it

The latest version of the YAML Cocoa parser is available form the Syck github.