Ah, yes. Objective-C, the programming language that once ruled the world of iOS development (you know, before the cool kids on the block named Swift and SwiftUI showed up). For those who are feeling nostalgic or strangely curious, let's dive into this ancient relic and unearth its quirks and eccentricities.
Objective-C is an offspring of C, with a mix of Smalltalk thrown in for that extra spark of complexity. The language was created by Brad Cox and Tom Love back in the 1980s. Props to them for giving us a language that – while being clunky and confusing – paved the way for modern iOS development.
Now, let's talk about some of the reasons we love (and by love, I mean tolerate) Objective-C:
Syntax:
Objective-C is like that eccentric uncle you try to avoid at family gatherings. It insists on doing things its own way, even if it means taking the path less traveled (and not always in a good way). For instance, let's look at the syntax for method calls:
[self performSelector:@selector(doSomething) withObject:nil afterDelay:2.0];
That's right, folks! Square brackets and colons galore. Shoutout to Smalltalk for inspiring this highly intuitive approach.
Memory Management:
Once upon a time, when dinosaurs roamed the earth and Objective-C was all the rage, we had to manually manage memory. Some might argue that this is akin to the dark ages, but it had its charms (or so they say). The days of retain
, release
, and autorelease
were truly magical times when you would be kept up all night chasing memory leaks. With the introduction of ARC (Automatic Reference Counting), life became a tad easier, but let's never forget the struggles of our ancestors.
Verbose Naming Conventions:
In Objective-C land, verbosity is king. Why keep things concise and readable when you can engage in a game of keyboard gymnastics?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
}
True art, right there.
Bridging Header:
Can't decide whether you want to stick with Objective-C or dive head-first into the world of Swift? No problem! The bridging header is here to ensure that you stay friends with both (while secretly giving you a slight headache):
#import "YourObjectiveCFile.h"
Who doesn't love a little extra complexity in their lives?
The Beauty of nil
:
Objective-C has an interesting relationship with nil
. It may seem strange at first, but it does have its advantages, like avoiding countless crashes due to null pointer exceptions:
NSString *name = nil;
NSLog(@"Hello, %@!", name); // Prints "Hello, (null)!"
This is Objective-C's idea of being "helpful."
Dynamic Messaging and Runtime:
If you thought Objective-C was all eccentric syntax and bitter sarcasm, think again! The language has some powerful features up its sleeves, such as dynamic messaging and a rich runtime that allows us to bend the rules and do things like swizzling:
+ (void)swizzleMethod:(SEL)originalSelector withMethod:(SEL)swizzledSelector {
Method originalMethod = class_getInstanceMethod(self, originalSelector);
Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
BOOL didAddMethod =
class_addMethod(self,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(self,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
Sure, some might argue that it's an invitation to chaos. But hey, with great power comes great responsibility, right?
So there you have it – a trip down memory lane with Objective-C, the language that, despite its quirks and foibles, played a pivotal role in shaping the world of iOS development. It may be (thankfully) fading into obscurity, but its impact will never be forgotten (no matter how much we might want to).
Grok.foo is a collection of articles on a variety of technology and programming articles assembled by James Padolsey. Enjoy! And please share! And if you feel like you can donate here so I can create more free content for you.