Objective-C Syntax

July 20th, 2008

I’m still trying to learn Objective-C and Cocoa here, and coming from Ruby, its an interesting experience.

Method calling Syntax

I simultaneously love and hate the method calling syntax. Take this for example:

[UIColor colorWithRed:1.0 green:1.0 blue:1.0];

This create an instance of the UIColor class with red green and blue values as specified, in this case white. I do like that the parameters are labeled, and I like that better than simply passing in a list of anonymous argument that most languages give you.

But what I don’t like is the method name being forced to mingle with the arguments. Semantically, red, green, and blue are all equal things. However I am forced to prepend the declaration of the red value with this colorWith prefix. The named parameters actually are the method name. According to the langauge internals, the name of this method is:

colorWithRed:green:blue:

Here’s how I might do the same thing in ruby:

UIColor.new(1.0, 1.0, 1.0)

Although, this requires some formal knowledge that the order of arguments is red, then green, then blue. But I can easily remember this since its a basic language feature.

Usually I don’t mind long method names, but when handling things as simple as initialization, it seems like I should have to look up the syntax. Long ruby method names aren’t too much of a problem, because they only have one part. Objective-C methods tend to be more verbose, and its hard to know where the arguments go and what type the arguments are supposed to be.

The result is that I can’t possibly imagine writing Objective-C without a code completing IDE. I have written huge amounts of ruby in Textmate, and really don’t miss code completion much. Maybe I just have to get used to the IDE mindset.

Objects and non-Objects

Ruby has spoiled me. Everything is an object in ruby. A string, a number, a chunk of code, everything. Even a class is an instance of the class Class.

In Objective-C the story is different. An object is an instance of a class that inherits from the NSObject class. Basically this means that the only things that are objects are things that are provided by Cocoa. Other things that are provided by the underlying C, like floats, strings and whatnot are not objects. Although, to be fair, Cocoa provides object based wrappers for most of the C non object primitives, but its not always worth the overhead to use them for simple stuff.

This makes the syntax a bit of a mess. The bracket method calling syntax only works on objects, because a non object (I dont even know what to call it) cant have methods at all. And then you have calls to C functions like:

UIGetCurrentGraphicsContext()

Which doesn’t even use the bracket syntax at all. I’m sure I will get used to all this, but I can’t help but feel this language would be a lot more fun if everything was an object. I think most people go from something like C to ruby, but I am going the other way around, and it’s odd. We’ll see where this goes.

Learning Objective C

March 8th, 2008

With the release of the iPhone SDK, it’s clear that I need to learn some Objective C. I have been meaning to pick it up for a while now, if only to write Mac OS X Desktop Apps. But coming from Ruby, and not knowing C, its a little weird. I have a brain dump of initial thoughts as I trudge through my learning process that I will document here.

First of all, memory management? Ruby has way spoiled me here. Having to manually allocate and deallocate memory manually seems like a huge pain the ass. But on the other hand, maybe that is one reason that Rails apps take need 50MB of RAM just to be present.

The bracket method invocation syntax is pretty weird to me. As is the method declaration syntax. I’m getting used to it, I am starting to see why it’s done that way. But its still ugly.

1
2
3
4
5
// method declaration
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

// method invocation
[myObject touchesBegan:someTouches withEvent:someEvent]

The “every argument is named” thing, is actually pretty nice. But the syntax to hook all that up, and strongly type every argument, is pretty ugly.

Lastly, for now, the language just seems needlessly verbose. For example, to create a new Photo:


photo1 = [[Photo alloc] init];

I much prefer ruby’s Photo.new. Or to get objects out of an NSArray object you actually have to do something like:


[myArray objectAtIndex:123]

Learning a new language is a great mind expanding exercise. But I hope to, one day, learn a language that didn’t make me wish I was using ruby instead.