Objective-C for fun and (some day) profit

It’s not much to look at, but I am in the process of moving all of my Objective-C, C, C++, Java, and JavaScript toy boxes over to Github. A toy box is what I call a workspace filled with unrelated, incoherent mini-programs that demonstrate language features. The goal here is to understand syntax, semantics, and language behavior.

One of my favorite features of Objective-C is that sending a message to nil (IE calling a method on a null reference in any other language) has the following effect:

It does nothing!

This allows for some elegant code. Algorithms look bloated when they check for null values and other edge cases. Consider binary tree traversal. The basic algorithm of an inorder search is :

Search : Tree
Search : Left Subtree if not null
Print Root Node Value
Search : Right Subtree if not null

Wouldn’t it be nice if we didn’t need to check for nil/null? In Objective-C, it is! Check this out :

@interface BT: NSObject {
        @public
        int val;
        BT *left;
        BT *right;
}
- (void) inOrder;

@end

@implementation BT
- (void) inOrder {
        [left inOrder];
        printf("%d\n",val);
        [right inOrder];
}
@end

Isn’t that cool??? The full code is available on Github.

Swift is up to version 3, but I still see Objective-C everywhere. I think Objective-C still a useful language to know.