Learning Cocoa

I’ve been spending a considerable amount of time trying to learn Objective-C and especially Cocoa.


After some searching around, I found tons of references to Cocoa Programming for Mac OS X by Aaron Hillegass. It’s a good tool for learning how to build applications with Apple’s development tools and frameworks, though it is missing some of the newer technologies.

I made it through the book and feel a bit better about what is going on, but I never really feel comfortable with a language or framework until I use it in an actual project. I’ve learned a ton of programming languages and frameworks through my years as a professional developer, but I think Objective-C and Cocoa are the hardest so far for me.

The first thing that I struggled with is the strange looking syntax that Objective-C uses to call methods of a class.

For example to call the setHealth method of an instantiated player object and pass in the value of 1, I would do the following:

[player setColumn: 1];

What really gets hairy and hard to read when you are not used to this syntax is when passing in multiple arguments to a method:

[player setPositionAtColumn: 1 andRow: 1];

And just to make it even harder to read, let’s move the player over one column:

[player setPositionAtColumn: [player column] + 1 andRow: 1];

It’s probably just a matter of getting used to the syntax, but boy does it really look and feel foreign to me. Though, I must admit, I really like the way that each method parameter is specified outright with a part of the method name. If you compare the next two method calls, which is more clear on what is happening?

[player initAtColumn: 1 andRow: 1 withHealth: 100 andScore: 0];

player.init(1, 1, 100, 0);

On top of the extremely foreign syntax, the Cocoa framework is huge! Finding out how to do things is an adventure sometimes. It’s one thing to have a book walk you through step-by-step, but when it comes time to actually program something from scratch while looking at the blank screen in the Xcode editor, I feel a bit lost.