Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

Learning Objective-C

Learning Objective-C

1,109 2
Well now that I have a iMac I have been working though a Objective-C/Cocoa book to wrap my head around the main language used by Apple Products. My initial impressions of the language was WTF why can't they just use C++ like other platforms. As I go though the book however, my opinions have been changing. For those that don't know Objective-C is basically a layer on top of the C compiler to introduce object oriented programming. You say why C++ does that. Yes C++ does that but not in the way Objective-C is. Objective-C is what I like to call a compiled static dynamic language hybrid. One of the most powerful features of Objective-C is the capability to modify code on the fly with code right down to the basic objects that are at Cocoa's core. Ruby programmers would know this as monkey patching. First a word of warning Objective-C is worthless on other platforms besides apple platforms because the reason objective-c is so powerful is because of the Cocoa framework that apple provides. Without Cocoa Objective-C is just another C + Objects language.

Now the most confusing thing for me with OC at the moment is the syntax. When you have an object you call its methods with the bracket syntax. For example...

OBJFoo* foo = [[OBJFoo alloc] init];
[foo sayHello];
[foo release];



The basic gist is [ ] in OC methods are called messages go figure damn IT types love their fancy words.

So lets say foo has a property call helloText. We can set this text using the set message and we can get the text using the get message.

OBJFoo* foo = [[OBJFoo alloc] init];
[foo setGreeting:@"Hello Foo"];
NSLog(@"%@", [foo greeting]);
[foo release];


With the new version of objective-c you can now use dot syntax to get at the accessor methods by saying foo.greeting = @"Hello Foo"; and foo.greeting to get the message.

This just adds to the confusion in my opinion. Just when I was understanding that everything in objective is pretty much a message and got use to reading the bracket syntax they throw this dot syntax at me. Yes I understand dot syntax but you can't use dot syntax for messages in general only accessors aka properties. So I have personally started converting all the code in the book to use brackets because for me it is easier to understand objective-c code with the brackets. Take this for instance...
Say we are using this window object inside of a class only so we prefix with self to make more sense.

NSWindow* myWindow = [[NSWindow alloc] init];
NSButton* myButton = [[NSButton alloc] init];
[[[self myWindow] contentView] addSubview:myButton]; // this is more understandable then
// this
[self.myWindow.contentView addSubview:myButton];



The reason I find it more readable is because
[[[self myWindow] contentView] addSubview:myButton] reads perfectly from left to right
get my instance of the myWindow object and then get its contentView now add the my button to our array of subviews.

the dot syntax
[self.myWindow.contentView addSubview:myButton];
is harder to understand what it is doing unless you 100% understand that the dot syntax is only used for properties and or instance variables.
yea the dot syntax makes the code look a bit cleaner but at the same time it is easy to glance at it and forget what is going on especially if you are not familiar with the underpinnings of objective-c. I think apple should have let this one out for consistencies sake.

Despite my syntax hiccups I am really starting to like the language overall as a whole. It is very well designed especially because of the amazing Cocoa framework.
I understand I did not go into detail on a lot of stuff because in all honesty I am just starting to get over the few hurdles the language throw's at me. But rest assured you will see more. My first project in the coming future is going to be a RSS reader. I understand that this is a game dev site but who cares it will be a interesting write up.

Discussion

Loading comments...