Objective-C is mainly used for developing software on Apple’s platforms, like macOS and iOS. Its syntax is notoriously difficult, combining elements of C with Smalltalk-style messaging. On paper, that sounds innovative. In reality, it’s a mess.
Concatenate two strings in Objective-C
This is unnecessarily verbose. Compare it to Swift.
Swift is concise and clear. Objective-C, on the other hand, feels like you’re fighting with the language to do something simple.
Method Calls
Method calls in Objective-C are like reading a ransom note.
Look at all those brackets and colons. It’s hard to read, and it’s easy to make mistakes. In contrast, Swift is straightforward.
Notice how much cleaner that looks. Swift’s syntax is modern, clear, and more like what you’d find in other languages. Objective-C feels like it was designed by someone who wanted to be different just for the sake of being different.
Memory Management: A Relic of the Past
Objective-C’s memory management used to be manual. You had to manage memory with retain, release, and autorelease.
It’s clunky and prone to errors. Forgetting to release an object results in memory leaks. Retaining it too much causes crashes. This is a relic of a time before modern memory management.
Thankfully, Automatic Reference Counting (ARC) was introduced. But even with ARC, the language still shows its age. Compare this to Swift, where ARC is integrated seamlessly.
Inconsistent Naming Conventions
Objective-C’s naming conventions are all over the place. Method names are long, descriptive, and sometimes too verbose.
There’s nothing wrong with being descriptive, but Objective-C takes it too far.
The result is code that’s difficult to read and maintain. Swift, by contrast, uses shorter, more consistent naming:
Swift’s method names are clear but concise, making the code easier to read.
Lack of Modern Features
Objective-C lacks many of the modern features found in newer languages. For instance, it doesn’t have proper support for:
- Generics: Handling collections of a specific type is awkward.
- Optionals: Dealing with null values is painful.
- Type Inference: You have to declare types everywhere.
Here’s how you’d declare a generic array in Objective-C:
And here’s how you’d do it in Swift.
In Swift, the syntax is cleaner, and you can often omit the type declaration because Swift infers it for you.
Working with Collections: A Tedious Affair
Working with collections in Objective-C is tedious. Let’s say you want to iterate over an array of strings.
It’s not terrible, but it’s not great either. Now, look at the Swift version.
Swift’s version is not only cleaner, but it also benefits from type inference, making the code less verbose. You don’t have to declare the type of str explicitly; Swift figures it out for you.
Working with Blocks: A Confusing Endeavor
Blocks (closures) in Objective-C are another area where the language shows its age. Here’s an example of a block that adds two numbers.
Compare that to the equivalent Swift closure:
Swift’s closure syntax is more intuitive and less cluttered. The Objective-C version is hard to read, and the syntax feels bolted on rather than integral to the language.
The Ugly Legacy: Header and Implementation Files
Objective-C splits code across header (.h) and implementation (.m) files. This is a relic from C, and it’s an outdated practice that clutters the codebase.
Header File (MyClass.h):
Implementation File (MyClass.m):
In Swift, this would be combined into a single file:
Swift reduces boilerplate and keeps everything in one place, making your code easier to navigate and maintain.
Here’s a classic Objective-C class that illustrates the verbosity and complexity of Objective-C syntax, especially when compared to modern languages like Swift.
Person.h (Header File)
Person.m (Implementation File)
Contrast with Swift
In Swift, the same functionality can be achieved with much less code, in a single file, and with a clearer, more modern syntax.
Why We Should Move On
Objective-C was revolutionary in its time. It brought object-oriented programming to Apple’s platforms and powered apps for decades. But today, it’s an abomination. It’s ugly, clunky, and difficult to work with.
Swift is the future. It’s modern, clean, and designed with today’s developer in mind. It’s time to leave Objective-C in the past where it belongs.
If you’re still using Objective-C, consider switching to Swift. Your eyes and your sanity will thank you.