If you started your iOS Development learning journey after 2014, you might be more familiar with Swift; Apple’s beloved programming language for their iOS, iPad OS, Apple OS, and TV OS operating systems. This is fine because most Mobile Engineering teams prefer to work with it due to its developer-friendly syntax, enriched APIs, and low learning curve. In this article, I’m going to share some basic concepts that can help you work on a codebase that has a mixture of Objective-C and Swift(this is extremely common for most iOS Applications).
- Differences between Swift & Objective-C.
- Basic Objective-C concepts.
- Intermediate Objective-C concepts.
Let’s first see a few ways that both languages differ(these are just a few ones):
- Since Objective-C inherits the C language, you need to keep track of both a header file(the “h” file) and an implementation file(the “m” file). Swift only requires a single file to define and implement a class.
- Objective-C uses a combination of static and dynamic typing, meaning that the majority of types of objects are declared at compile type.
- In Objective-C, mutability is limited by certain classes and property attributes, and by default, all objects are mutable. NSString, NSArray, NSDictionary, NSSet are not mutable, To mutate objects of that type, you must use NSMutableString, NSMutableArray, NSMutableDictionary, etc.
- Swift uses the standard true/false for its boolean evaluation, while Objective-C uses a combination of YES/NO and Boolean type.
- Objective-C requires a semicolon at the end of each statement(yeah, that annoying thing🙃) while Swift does not.
- Swift has the concept of type inference, while Objective-C requires you to explicitly declare the data type of a variable.
Now that we’ve seen how Objective-C differs from Swift, let’s take a look at some basic concepts that’ll get you familiar with its ecosystem.
1. Variables & Printing Statement
2. Primitive Data Types
Like many other programming languages, Objective-c has its main primitive data types. They are(maybe be missing a few but these are the core of it):
3. Function Declaration
Another major difference between the two languages is how we declare and define functions. Swift has very clean and intuitive function definitions such as Kotlin, whereas objective-c takes precedence of languages such as C/C++.
1. Object-Oriented Programming
While the core principles of Object-oriented Programming remain the same in Objective-C, there are a few things that take a slightly different approach. Objective-c takes the concept of a header(also known as h-file) and implementation(also known as m-file) file. The idea here is that we have one file for just declaring our class(the name, the properties, and methods) and the other for implementing our class. Let’s take a closer look:
The actual implementation of the class will look something like this:
The next step is to declare and manipulate the instance of the class above:
Every enum is of type NSInteger and there is no “case” keyword to represent each property.
As you just read, there are some significant differences between Objective-C and Swift. Though it might feel unnecessary to learn Objective-C when Apple fully supports Swift as its primary programming language, I believe doing so can help you in a few ways, one being that most mobile applications were built out pre-swift, meaning that their core and legacy functionalities and files such as the AppDelegate are still in Objective-C. Thanks for stopping by! If you learned a thing or two in this article, don’t forget to support by leaving some claps 👏