O.G. O.C.

Tom Zurkan
4 min readSep 8, 2023

--

Objective-C has been around for a while and I love working with it. My one complaint is it is so verbose (lots of typing). But, the verbosity has named parameters. That feature didn’t seem to come into other languages till later. What else makes Objective-C O.G.?

Objective-C was developed in the 1980’s and was used by Steve Jobs company NextStep. NextStep was eventually bought by Apple and OSX was born. As a matter of fact, you may not know that the prefix NS is from NextStep. And of course Next Steps OS was based on the mach micro kernel (unix).

The most obvious Objective-C feature is the optional. You can access an optional method with brackets as in [object method]. If the object is nil, the method is a noop (no operation). This really makes code look nice and clean with less checking for nil every statement. The equivilant of optional chaining.

Another feature I might point to would be the literal. Ok, so you have literal string with @”here”. But, you can also use that for other literals like dictionaries, lists, and NSInteger:

NSArray *arr = @[@”1", @”2", @”3"];

NSDictionary *dict = @{@”1":@1, @”2":@2, @”3":@3};

That is pretty convenient and simple don’t you think?

So, those are a couple of interesting features but let’s go deeper. We can’t forget about ARC (automatic reference counting). That might sound trivial, but, it is pretty awesome to not have to keep track of “ownership” through reference counting. It’s definitely one of the cooler features of Objective-C.

But, there’s more. I have always said about Java that things like access modifiers are more suggestions than ridgid rules. You can use reflection to modify those modifiers. Objective-C also gives you flexibility. It has class method addition as well as something called swizzling where you can actually replace the functionality of an existing method in a class.

Below I have provide a simple example of taking a vanilla object and using something called the associated object, you can essentially add getters and setters. Below, I just used methods to make it easy. I also added them in the init which isn’t ideal.

NS_ASSUME_NONNULL_BEGIN

@interface Player : NSObject

@end

NS_ASSUME_NONNULL_END
#import "Player.h"
#import <objc/runtime.h>

@implementation Player
- (instancetype)init {
if (self = [super init]) {
// Initialize self
//Class clazz = objc_allocateClassPair(self, "Player2", 0);
BOOL v = class_addMethod([self class], @selector(setMyObject), (IMP) setMethodIMP, "v@:@");
v = class_addMethod([self class], @selector(getMyObject), (IMP) getMethodIMP, "@@:");


}
return self;
}
void setMethodIMP(id self, SEL _cmd, id val)
{
// implementation ....
objc_setAssociatedObject(self, "myNewObject", val, OBJC_ASSOCIATION_RETAIN);
}

id getMethodIMP(id self, SEL _cmd)
{
// implementation ....
return objc_getAssociatedObject(self, "myNewObject");
}
- (void)dealloc {
objc_removeAssociatedObjects(self);
}
@end

I took a vanilla object and added a method for setting and getting a new object. The associated object is a associated map that comes with each NSObject and allows you to add variables by using the associated map along with augmenting the class.

    Player *p = [[Player alloc] init];

SEL sel = @selector(setMyObject);
NSString *str = @"Objective-C is powerful !";

[p performSelector:sel withObject:str];

id val = [p performSelector:@selector(getMyObject)];

Since runtime supports performSelector, you can call methods at runtime by name without knowing much about the actually class or object. As you can see above, the performSelector can be used to call methods that have been added at runtime. Pretty cool!

You can also use categories to create extensions for existing classes. The category basically adds the method to the class at compile time instead of runtime. But, you could see how you could use associated objects along with categories to add some neat features to classes in other packages.

There are also key value observing that can be added at the NSObject level to be notified when an objects key value has changed. The key value observer pattern is a very powerful feature.

Lastly, let’s not forget blocks as objects (They are like functions but can be assigned to variables and passed as arguments). The completion handler is a pretty constant pattern in Objective-C.

As I had mentioned earlier, one of my complaints about Objective-C is it can be very verbose. But, that is also one of its strengths. You have named parameters which makes readability and parameter information pretty straight forward.

I’ve listed quite a few things about Objective-C that make it the original. You can do alot with Objective-C. But, with great power comes great responsibility. You can definitely run into problems if you misuse it. If you haven’t taken Objective-C for a spin, try the language out, you may find it a great gateway to C. Happy Coding!

--

--

Tom Zurkan

Engineer working on full stack. His interests lie in modern languages and how best to develop elegant crash proof code.