FREN

#FF00AA


1 feb. 2006

Objective-C First Contact

@misc@

It all started on Windows 3.1, with Borland C++. The language was simple enough, the Borland classes were pretty well thought out (I never had the misfortune of touching the MFCs), and for the time it was a rather good development environment. (Ironically, most of my time programming Windows was spent working on a NeXTstep interface clone — at a time when Windows customization and OS emulation weren’t as common as it is now.) Later on, I tried BeOS for a bit, and you could tell it was really designed from scratch to be programmed easily, unlike a certain antiquated Microsoft system. But you know what became of BeOS, and I only started programming again when .NET was beginning to get traction. Although it could get really hairy whenever you wanted to do something that wasn’t specifically included in the .NET classes and had to reach for the Win32 APIs, C# was amazingly convenient to write: just like C++, only simpler, with garbage collection and all. Fun! (That’s when I made Ghrone, a skinnable translucent desktop clock that isn’t obsolete yet.) Oh, and obviously I routinely do PHP, too.

At the time I began programming for Windows, the computer magazines (and that was when there were only a couple of them) were filled with stories about NeXTstep, how it was modern and well designed and a breeze to program and a developer’s paradise, and it was going to revolutionize application development. Needless to say, when I finally got a Mac almost twenty years later, knowing that OS X had inherited so much of its programming platform from NeXT, I was filled with expectation. Until I started eyeballing Objective-C code.

Oh boy.

I guess I approached it from the worst possible angle: a quick C++ to Obj-C tutorial (more of a cheat sheet, actually) that shows how to write Objective-C when you’re coming from a C++ world. What the hell was that now? Brackets all over the place, and yet there’s still some C syntax around? If there’s still C in there, why is the syntax for objects and methods so creepy? And what’s with method definitions? How could that ever make any sense to anyone? Those method calls? This is all designed by a madman!

I guess the secret to learning OS X programming is, don’t start out that way. History is your friend; I found it helped a lot to realize that Objective-C was designed many years ago (roughly at the same time as C++, but with a very different philosophy) and that you can’t blame it for being less advanced and modern than C#, which was born twenty years later. And that the insane syntax stems from a decision to keep the language and compilers 100%-compatible with C. And that the NeXT engineers didn’t choose it because the syntax was cute, but because it has very advanced and intelligent mechanisms for simplifying the programmer’s life on complex projects, only they’re not really obvious at first glance.

I haven’t really started programming it, as I’m still knee-deep in documentation (I have this weird habit of reading all the docs before I get started, rather than try out examples as I go — I know you’re not supposed to do that, but it’s always worked for me), but I must say the way the language replaces garbage collection (so very convenient in C#, but admittedly a bit too bloated for computers of the 1980s) with “reference counting”, and adds subtleties like ‘autorelease’, is pretty clever — it took twenty years for Windows programmers to get rid of the “whoever is supposed to delete this pointer that I sent/received?” dilemma, and even now the .NET classes aren’t extensive enough to completely shield you from those system calls moving pointers around. After .NET and PHP I still can’t believe I’m going back to programming without a garbage collector or equivalent, having to keep track of my pointers, but I feel much better knowing the language conceptor had a clear and clever idea of how to help managing them — smart enough that I don’t feel the urge to wait until garbage collection comes out in Leopard.

I’m still not a fan of some discrepancies between .NET and Cocoa development — .NET and Visual Studio are much more automated, and it’s really weird to see Interface Builder doesn’t generate a single line of code (by design, it turns out) — but despite my initial reaction I now do look forward to getting my hands in it. I only need enough free time, which will be a tad harder now that I have rent to pay.

 

[02/02] Here are some pointers to help you if you’re going from C++ to Obj-C, in the order I read them:

  • The C++ to Obj-C cheat sheet — like I said, lacks explainations and disregards everything that’s conceptually different, but you’ll be roughly able to read Obj-C code, and that’ll allow you to understand the rest more easily

  • Wikipedia has a good page teaching you a bit on Obj-C (although it’s not always 100% clear)

  • More about memory management in Obj-C on MacDevCenter and Stepwise (that seems like the most important thing to learn when you’re coming from another language)

  • The other articles in the “Cocoa Basics” section on Stepwise, with good information (you’ll notice some articles date back to the very first OS X beta days, so some details may be obsolete, but not the basics)

Hope this helps.

Want to know when I post new content to my blog? It's a simple as registering for free to an RSS aggregator (Feedly, NewsBlur, Inoreader, …) and adding www.ff00aa.com to your feeds (or www.garoo.net if you want to subscribe to all my topics). We don't need newsletters, and we don't need Twitter; RSS still exists.

Legal information: This blog is hosted par OVH, 2 rue Kellermann, 59100 Roubaix, France, www.ovhcloud.com.

Personal data about this blog's readers are not used nor transmitted to third-parties. Comment authors can request their deletion by e-mail.

All contents © the author or quoted under fair use.