Learning Objective-C

Published January 30, 2011
Advertisement
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.
0 likes 2 comments

Comments

Ravyne
If nothing else its an interesting direction for someone to have taken C in. I've often described Objective-C as the language that the AI computer in The Jetsons must have been written in -- A language designed for a future that hasn't yet happened :D, and where every message has the possibility of being an RPC call transparently.

One thing I read recently that has softened my oppinion of Objective-C is that the direction it took off from C is inspired by smalltalk and also by lisp in some respects, which certainly helped me see their design decisions from the correct perspective. C++, instead, opted to go down the path of "logically extending" Vanilla C's syntax into an OO world, and so they ended up with a much different result. I certainly don't intend to write more of my software in Obj-C since anything I write for iOS/MacOS needs to have a core that's portable to at least windows, but this recent revalation will at least have me taking a little more joy in writing the parts that need to be done or interact with Objective-C (Cocoa/UI, etc).
January 31, 2011 02:25 AM
blewisjr
I definitely agree here. Personally I really like the direction objective-c went. I truly is very refreshing and much better done then C++. C++ made the syntax quite complicated which induces a lot of errors. Objective-C like you said when the small talk / lisp route and it churned out something very elegant that is very easily extensible with strait C where needed. Not to mention it is very easy to tell what is what. Now if only the Cocoa framework was cross platform I think we would have a winner of a language.
February 02, 2011 12:38 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement