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.
March 8th, 2008 at 03:33 PM
This is part of the problem with Objective-C. Obj-c now has GC but good luck finding book that covers the new changes.
March 9th, 2008 at 12:57 PM
Don’t get too comfortable with garbage collection if you’re thinking about coding for the iPhone, as it doesn’t support it. You have to release and retain manually … ugh.
March 24th, 2008 at 01:10 AM
Sorry for bothering you with fleximage related question, but could you tell me how to retrieve the width and height of an image to use it in custom processing?
My complete question is here (if you fell you are specially patient for noob questions today):
http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/170313668581559c/a67b29a882079995?hl=en#a67b29a882079995
Thank you for your patience and for creating this awesome plugin
September 7th, 2008 at 03:56 AM
Objective-C does have garbage collection, so you don’t actually need to allocate or free memory at all.
photo1 = [[Photo alloc] init]; is analogous to: photo1 = [Photo new];
The reason it’s “needlessly verbose” (it’s actually necessary) is because it’s a superset of C, and C isn’t Object Oriented. There’s also no operator overloading, so you can’t use foo[index] syntax. Once you get to know ObjC, it gets a lot more sexier.
Oh and Objcetive-C isn’t strongly typed… you could just as well define things like:
- (void)touchesBegan:(id)touches withEvent:(id)event;
but that slows things down and is error prone.
September 9th, 2008 at 09:37 AM
Objective-C does have garbage collection, so you don’t actually need to allocate or free memory at all.
Yes, it does. But sadly, it’s not supported on the iPhone at all. This makes me sad.
The reason it’s “needlessly verbose” (it’s actually necessary) is because it’s a superset of C, and C isn’t Object Oriented. There’s also no operator overloading, so you can’t use foo[index] syntax. Once you get to know ObjC, it gets a lot more sexier.
I think that’s the biggest fault I have with the language. I’m liking it more than I used to, but it still feels like an object oriented addon to a non object oriented language. The fact there is even a difference between methods and functions just bothers me.
Oh and Objective-C isn’t strongly typed…
You are correct. However, that’s not the best practice. And XCode doesn’t make dynamic typing easy for you; it often warns “This object may not support method foo:bar:baz”, even though it then compiles and works fine. And using Cocoa without code completion is just WAY too hard.
January 2nd, 2009 at 07:25 PM
I started trying out ObjC recently too. I agree with almost all of your initial impression.
The last point on code complete made me want to crawl back to Eclipse. It seems MS Visual Studio is more helpful than XCode…
February 4th, 2009 at 11:02 PM
Do you know where i can find a eclipse plugin for Object-C???