Objective C, a brief history

Even in 2024 it is important for Swift developers to keep improving their skills and expand their knowledge on Apple Development. Even in 2024, a lot of iOS and MacOS applications still contain Objetive C code. So with this new Objective C guide, we want to help you expand your knowledge and improve your development skills!

article

Even in 2024 it is important for Swift developers to keep improving their skills and expand their knowledge on Apple Development. Even in 2024, a lot of iOS and MacOS applications still contain Objetive C code. So with this new Objective C guide, we want to help you expand your knowledge and improve your development skills!


For who is this Objective C Guide?


So while Swift has mostly taken over the development for Apple platforms, a lot of older applications and Apple frameworks still use some Objective C or are still written in Objective C.


This guide is for Swift developers who have a good understanding of the Swift programming language and want to expand their knowledge and increase their value as a developer for Apple products. After this guide, you will have a good understanding of how Objective C works and how you can apply it yourself in your or client applications.


What is Objective C?


Objective-C is a programming language that combines the power and elegance of C with the added capability of object-oriented programming. It was the primary language used by Apple for macOS and iOS development before Swift took the spotlight. Understanding Objective-C is crucial for iOS developers, especially when working with legacy code or certain frameworks that are still written in this language.


The Origins of Objective-C


Objective-C was created in the early 1980s by Brad Cox and Tom Love at their company, Stepstone. The language was designed to bring object-oriented programming to the versatile and widely-used C language. Cox and Love were inspired by Smalltalk, one of the earliest object-oriented languages, and they wanted to integrate its features into the C programming language, which was already popular for system and application software.

The Advantages of Objective-C Over C


OOP (Object Oriented Programming)


The primary enhancement that Objective-C offers over C is its object-oriented programming (OOP) capabilities. In C, developers work primarily with functions and data structures, which can become complex and difficult to manage in large programs. Objective-C introduces the concept of objects, which bundle data and methods (functions) into reusable units. This makes programs more modular, easier to understand, and more maintainable.


Dynamic Runtime


Objective-C features a dynamic runtime, which means many decisions that other languages make at compile time are deferred until the program is running. This allows for more flexibility and dynamic behavior in applications. For example, methods and classes can be added or changed at runtime, enabling powerful features like method swizzling (changing method implementations on the fly) and dynamic method resolution.


Messaging Syntax


Objective-C introduces a unique messaging syntax that is different from the function call syntax in C. Instead of calling functions, Objective-C sends messages to objects. This is closer to how humans naturally communicate and makes the code more readable. For example, in C, you might call a function like this:


    
        int result = addNumbers(5, 19)
    

In Objective-C, you send a message to an object like this:


    
NSInteger result = [calculator addNumbers:5 with:10];
    

This messaging syntax is a hallmark of Objective-C and facilitates clearer, more organized code.


Core concepts of Objective C


Classes and Objects


In Objective-C, a class is a blueprint for creating objects. A class defines properties (data) and methods (functions) that the objects created from the class can use. Here’s a simple example of a class definition in Objective-C:


    
@interface Person : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;

- (void)greet;
@end

@implementation Person
- (void)greet {
    NSLog(@"Hello, my name is %@ and I am %ld years old.", self.name, (long)self.age);
}
@end    

In this example, Person is a class with two properties, name and age, and one method, greet. An object is an instance of a class:


    
Person *person = [[Person alloc] init];
person.name = @"John";
person.age = 30;
[person greet];    


Objective C Inheritance


Objective-C supports inheritance, a key feature of object-oriented programming. Inheritance allows a class to inherit properties and methods from another class. This promotes code reuse and logical hierarchy. For example:


    
@interface Employee : Person
@property (nonatomic, strong) NSString *jobTitle;
@end

@implementation Employee
- (void)greet {
    [super greet];
    NSLog(@"I work as a %@.", self.jobTitle);
}
@end  


Here, Employee inherits from Person, meaning it has all the properties and methods of Person plus its own additional property, jobTitle.


Categories and Protocols


Objective-C provides categories and protocols to extend functionality and define interfaces, respectively. Categories allow you to add methods to existing classes without subclassing:


    
@interface NSString (ReversedString)
- (NSString *)reversedString;
@end

@implementation NSString (ReversedString)
- (NSString *)reversedString {
    NSUInteger length = [self length];
    NSMutableString *reversedString = [NSMutableString stringWithCapacity:length];
    for (NSInteger i = length - 1; i >= 0; i--) {
        [reversedString appendFormat:@"%C", [self characterAtIndex:i]];
    }
    return reversedString;
}
@end 


Protocols define a blueprint of methods that can be implemented by any class. They are similar to interfaces in other languages:


    
@protocol Drivable 
- (void)startEngine;
- (void)drive;
@end

@interface Car : NSObject 
@end

@implementation Car
- (void)startEngine {
    NSLog(@"Engine started");
}
- (void)drive {
    NSLog(@"Car is driving");
}
@end


Objective-C in iOS Development


Before the introduction of Swift in 2014, Objective-C was the primary language for iOS development. Even today, many existing iOS applications and libraries are written in Objective-C, and understanding it is valuable for maintaining and improving legacy code.


Objective-C and Swift are interoperable, meaning you can use both languages within the same project. This allows developers to gradually transition from Objective-C to Swift or to leverage existing Objective-C codebases in new Swift projects. The interoperability is seamless, with bridging headers and runtime support ensuring that Objective-C and Swift objects can interact with each other smoothly.


Conclusion


Objective-C remains an important language for iOS developers, offering powerful object-oriented features, a dynamic runtime, and deep integration with Apple's development frameworks. Its legacy in the development community is substantial, and a solid understanding of Objective-C can enhance your capabilities as an iOS developer, especially when dealing with legacy codebases or complex projects requiring fine-grained runtime control. Despite the rise of Swift, the principles and practices of Objective-C continue to influence and shape modern iOS development.

instructor

Exodai INSTRUCTOR!

Johan t'Sas

Owner and Swift developer!