Archive for Juni, 2008

You are currently browsing the Martin en Neukoelln archives for Juni, 2008.


A new objective: Learn Objective-C

There are some rumors that say, you should learn a new programming language a year to stay fit and … Well for me, with the rise of the iPhone and my steady Mac usage it was clear that someday I must learn Objective C.

I tried it once a few month ago, but it was pain, really pain. So I left it until last week when I ordered two books on Objective C.


Programming Objective C

and


Cocoa

The first one is a complete introduction into programming including object oriented programming etc. This is a nice study to get the concepts and takes you about half a day to get through the first half of the book if you are an experienced programmer. The second half of the book is on the foundation framework in objective c which is the core library you normally use. It covers the most important topics and is quite usefull. The latter book is only on programming Cocoa und here mostly on programming graphical user interfaces than a more or less complete reference on the topic. The writing style is ok but not to expressive.

Cool Things

poseAs: allows you to somehow monkey patch a given class by subclassing it, overriding the wished method and posing as the super class. Other languages do open classes objective-c does poseAs:

Objective-C is dynamically typed – somehow. It allows you to pass the generic Id type to methods and functions, but dont expect the compiler to like this. Her will spit out a whole lot of warnings that things can go wrong.

Method missing is called forward and works as expected in Ruby or Python.

A Command Line Twitter

The question is how do you learn a language but without speeking it? Well you cant. So I needed a project that seems suitable to cope with the default API and is still easy enough to finish in a short time frame. The result was, that I started to develop a Twitter command line utility that allows you to post Twitter tweets directly from your favorite place to live.

Over all this worked quite well. Once you get used to XCode you can write code quite fast. But what stays with a wierd taste in your mouth is that it seems that you need your mouse to often and cant access the most actions via keyboard. For me as a heavy keyboard user this is bad.

The documentation is good, again once you get used to it. The way it is written directly reflects the categories defined in the protocols of the implementing classes. More example code is appreciated.

Conclusion

Objective-C is a nice language once you get used to writing lines as

 unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:
                    [NSData dataWithContentsOfFile:  [path stringByExpandingTildeInPath] ]];

with too much [ and ] on the road. But the way it is integrated in C is pretty neat and leaves you all your memory-optimization-pointer-role-play-games-in-the-dark possibilities. The other thin is the documentation problem. Coming from the Ruby side, you would expect things to work, which it does as well in the core libraries, but when it comes to simple tasks as opening a URL and posting data with Basic HTTP auth, it took me hours to get through the whole thing.

On the other hand the cool thing is that NSURLConnection uses in synchronous mode you current login keychain to see if you already have a basic auth stored and uses this. Unfortunatly this is not docuemented (at least I did not find it).

I will definitley develop further with Objective-C and Cocoa to build my first iPhone app and distribute it through the App store. ;) If I am further on the road I will write a new conclusion on the new things I learned.