Archive for March, 2008

Become an Xcoder is a free little eBook we wrote to help beginners with no or little programming experience to start their journey into the world of Mac OS X development with Objective-C and Cocoa. (See Learn Cocoa for a recent discussion on this topic).

I’m glad to announce that the book has been updated for Leopard and Xcode 3, thanks to the work of Alex Clarke (one of the original authors and our publisher at CocoaLab).

You can download the PDF of this new edition:

You can also access translations (not updated for Leopard yet) and a very nice HTML version of the book here at CocoaLab. For exploring more advanced Cocoa topics, Alex has created LabNotes, where you can find learning material, including hands-on exercises, on topics such as Cocoa bindings, MVC, accessors, etc.

So far the book has been quite successful, with nearly 100,000 downloads (which give me an opportunity to shamelessly brag about!). Interestingly, the number of downloads has increased in recent weeks. We have gone from an average of 400 downloads/week in January to 1000 in February to 2700 this month. I would not be surprised for this to be representative of a growing interest in Cocoa, amplified by the recent iPhone SDK announcement. I wonder if other Cocoa related web sites or publishers are seeing the same trend…
Anyway, enjoy Become an Xcoder, Leopard Edition!

Read Full Post »

Briksoftware has released Drop Inspector, a development utility that embeds an F-Script environment. Drop Inspector’s job is to let you inspect the contents of the pasteboard, including drag and drop data.

On Mac OS X, the pasteboard is a subsystem that allows sharing data between components or applications:

You typically use pasteboards in copy and paste operations, although pasteboards also provide the basis of system services. NSPasteboard objects transfer data to and from the pasteboard server. The server is shared by all running applications. It contains data that the user has cut or copied, as well as other data that one application wants to transfer to another. […]

Data can be placed in the pasteboard server in more than one representation. For example, an image might be provided both in Tag Image File Format (TIFF) and as encapsulated PostScript code (EPS). Multiple representations give pasting applications the option of choosing which data type to use. In general, an application taking data from the pasteboard should choose the richest representation it can handle?rich text over plain ASCII, for example. An application putting data in the pasteboard should promise to supply it in as many data types as possible, so that as many different applications as possible can use it. […]

The pasteboard owner declares the data types it can write. Pasteboard data generally refer to an object instance whether a string, an arbitrarily complex object graph such as a dictionary of arrays, an instance of NSData, or an object wrapper for an arbitrary block of data. You can name your own pasteboard types for special-purpose data types.

Drop Inspector provides access to pasteboards contents from an hexadecimal-view and the embedded F-Script environment. You get the full power of F-Script for inspecting and manipulating objects graphically. Nice!

Read Full Post »

The video of Tim Burks C4 talk has just been released.

Tim talks about Objective-C, Ruby/Objective-C bridges (including one he created), and his new baby, the Nu language. Nu is a dynamic language based on the Objective-C run-time, with a strong flavor of Lisp and Ruby.

Catch the other C4[1] videos here.

Read Full Post »

Here is a little list of things that, in my experience, contribute to make Objective-C a powerful and fun programming language.

Classes are objects

Each class is an instance of a meta-class automatically created and managed by the run-time. We can define class methods, pass classes as arguments, put them in collections and so on. To create an object, we just send a message to the class we want to instantiate. No need to reinvent a "factory" system. No need for a specific constructor mechanism at the language level. This helps keeping the language simple and powerful.
And, by the way, meta-classes are objects too!

Dynamic typing

As in Ruby, Python, Smalltalk, Groovy… Extremely useful because we don’t always know beforehand what our objects are going to be at run-time. Dynamic typing in Objective-C is simple to use. For example, this declares a variable that can hold a reference to an object:

id myObject;

Optional static typing

Still, Objective-C also has support for static typing. Best of both worlds.

This declares a variable that can hold a reference to an object of class (or subclass of) NSView:

NSView *myObject;


Categories let us define new methods and add them to classes for which we don’t have the source code (such as the standard Cocoa classes provided by Apple). This makes it easy to extend classes without resorting to subclassing. Extremely useful to adapt existing classes to the requirements of frameworks we want to use or create.

Message sending

We interact with objects by sending them messages. Often, the receiver of a message will have a method that directly matches the message (i.e. that has the same name, or, in Objective-C terms, the same selector). In this case the method will be invoked. But this is not the only possible outcome, as an object can choose to handle a message in other ways such as forwarding it to another object, broadcasting it to a number of objects, introspecting it and applying custom logic, etc.
Very powerful…

Expressive message syntax

Message patterns in Objective-C are like natural language sentences with holes in them (prefixed with colons). When we write code that sends a message to an object, we fill the holes with actual values, creating a meaningful sentence. This way of denoting messages comes from Smalltalk and makes the code very expressive.

Example, sending a message to an ordered collection, asking it to insert a given object at index 10:

[myCollection insert:myObject atIndex:10]

A message sending expressions can be read like a sentence where the receiver is the subject and the message is the rest of the sentence (for instance, an action that we would like the receiver to perform): "myCollection insert myObject at index 10".


Introspecting objects is easy. For example, we can ask an object for its class like this:

[myObject class]

Determine if an object has a method "foo":

[myObject respondsToSelector:@selector(foo)]

Ask an object for the signature of its method "foo":

[myObject methodSignatureForSelector:@selector(foo)]

Ask a class whether it is a subclass of another class:

[class1 isSubclassOfClass:class2]

And so on…

Dynamic run-time

Objective-C has a dynamic run-time. It allows crafting messages at run-time, dynamically creating classes, dynamically adding methods to existing classes, changing method implementations and so on…

For a nice example of what can be achieved with Objective-C introspection and dynamism you can have a look at this article I wrote about the graphical object browser provided by F-Script.

Automatic garbage collection

The automatic garbage collector runs in its own thread, concurrently with the application code. It uses a generational model to improve its efficiency by targeting in priority memory zones that are more likely to be garbage. It works for objects and also for raw C memory blocks allocated with the NSAllocateCollactable() and similar functions. malloc() works as usual, providing control over memory not managed by the collector.

The garbage collector is an opt-in service: you can choose to not make use of it in your application and instead rely on a reference counting system. This system includes a rather ingenious delayed release mechanism that goes a long way to reduce the burden of manual reference counting.

Note that at the time of this writing, the automatic garbage collector is not available on the iPhone.

C inside

Objective-C is primarily an object-oriented extension to the C language and constitutes a superset of C. This means that the raw power of C is available, and that C libraries can be accessed directly (as you know, there is quite a number of them available out there!). Beside, this creates a symbiotic relationship between the language and the operating system, as Mac OS X, which is a UNIX system, is primarily written in C and, for the upper-level parts, in Objective-C.

C++ fluent

Not only is Objective-C a superset of C but it can also understand and call C++ code. Used in this configuration, the language is named Objective-C++ and allows mixing Objective-C and C++ code in the same code statements. It also allows directly using C++ libraries.


Objective-C’s Smalltalk-inspired object system is leaning toward simplicity. Many features that tend to render languages complex (templates, overloading, multiple inheritance, etc.) are simply absent from Objective-C, which offer simpler programming models taking advantage of its dynamic nature.

Access to Apple technologies

Each new version of Mac OS X, and now, of the iPhone OS, is full of interesting new technologies which are directly available from Objective-C to play with. This contributes significantly to make Objective-C fun to use.

Read Full Post »

Ruby + ObjC = MacRuby

Laurent Sansonetti, Apple's Ruby wizard, has released MacRuby, an Apple open source project which unifies Ruby and Cocoa (instead of just bridging them like RubyCocoa does).

MacRuby is a version of Ruby that runs on top of Objective-C. More precisely, MacRuby is currently a port of the Ruby 1.9 implementation for the Objective-C runtime and garbage collector.

Laurent talks about the innards of MacRuby in this interview at InfoQ:

The Ruby object data structure had to be modified to conform to the Objective-C object data structure, so that a Ruby object can be casted at the C level as an Objective-C object. Then, the object allocator was modified to use the Objective-C one instead, which means that all objects (Ruby and Objective-C) are allocated from the same memory pool.

Finally, the traditional Ruby garbage collector was removed and instead we use the Objective-C garbage collector. This change wasn't very easy because the collector runs by default in generational mode, and expects you to appropriately set “write-barriers” every time you register an object in the object store, because it collects young generation of objects based on this information. [Read more here]

Read Full Post »