Objective-C Intro
Objective-C is a small, dynamic, object-oriented language built as a strict superset of C. Every valid C program is also a valid Objective-C program; on top of C it adds classes, objects and message passing, an idea borrowed from Smalltalk. For more than twenty-five years it was the main language for writing software on Apple platforms, and an enormous amount of macOS and iOS code (including much of Apple's own frameworks) is still written in it.
Here is a complete Objective-C program. Don't worry about the details yet: the next lessons explain every line.
#import <Foundation/Foundation.h>
int main(void) {
@autoreleasepool {
NSString *language = @"Objective-C";
NSInteger year = 1984;
NSLog(@"Hello from %@, born in %ld!", language, (long)year);
}
return 0;
}Hello from Objective-C, born in 1984!
A short history
| Year | Event |
|---|---|
| 1984 | Brad Cox and Tom Love create Objective-C at their company Stepstone, adding Smalltalk-style messages to C |
| 1988 | Steve Jobs' company NeXT licenses it and builds the NeXTSTEP operating system and its frameworks with it |
| 1996–97 | Apple buys NeXT; NeXTSTEP becomes the base of Mac OS X and its Cocoa frameworks |
| 2006 | Objective-C 2.0 adds properties, fast enumeration and a modern runtime |
| 2008 | The iPhone SDK opens Cocoa Touch (iOS) to all developers, and Objective-C becomes hugely popular |
| 2010–11 | Blocks (closures) and ARC (Automatic Reference Counting) arrive |
| 2014 | Apple introduces Swift, which gradually becomes the recommended language for new code |
The NS prefix you will see everywhere (NSString, NSArray, NSLog) is a leftover from NeXTSTEP.
Where Objective-C is used today
- Existing Apple apps: many large iOS and macOS apps still contain hundreds of thousands of lines of Objective-C.
- Apple's frameworks: UIKit, AppKit, Foundation and many others were designed in Objective-C, and their API shapes (delegates, selectors,
NSError **) make much more sense once you know the language. - Mixed projects: Swift and Objective-C can live in the same app and call each other, so teams migrate gradually.
- Low-level tools: because it is C underneath, Objective-C mixes freely with C (and, as "Objective-C++", with C++) libraries.
How it relates to C and Swift
Objective-C keeps all of C: int, for loops, pointers, structs and functions work unchanged. What it adds is recognizable by the @ sign and square brackets:
#import <Foundation/Foundation.h>
int main(void) {
@autoreleasepool {
// Plain C: variables, arithmetic, a loop
int total = 0;
for (int i = 1; i <= 4; i++) {
total += i;
}
// Objective-C: objects and messages in [square brackets]
NSString *word = @"messages";
NSString *loud = [word uppercaseString];
NSLog(@"C computed %d, Objective-C sent %@", total, loud);
}
return 0;
}C computed 10, Objective-C sent MESSAGES
Swift was designed as a successor: safer (no raw pointers by default, optionals instead of nil surprises) and with a more modern syntax. But Swift runs on the same frameworks, and Objective-C classes can be used from Swift and the other way round. The last lesson of this course shows how.
| C | Objective-C | Swift | |
|---|---|---|---|
| Objects and classes | no | yes | yes |
| Method calls | — | dynamic messages, decided at run time | mostly static, decided at compile time |
| Memory | manual malloc/free |
reference counting (ARC) for objects | ARC |
nil |
NULL pointer |
sending a message to nil does nothing |
optionals checked by the compiler |
About the playgrounds in this course
No Objective-C compiler runs inside a web browser, so the objc examples on this site are simulated: pressing Run shows the saved output. Every saved output was produced by really compiling the program with clang (with ARC and blocks enabled) against GNUstep, an open-source implementation of Apple's Foundation framework, on Linux. Because of that:
- everything in Foundation (strings, numbers, arrays, dictionaries, errors, notifications) behaves as on a Mac;
- a few printed descriptions look slightly different from Apple's, so the examples usually print values themselves;
- UIKit, AppKit, SwiftUI and Grand Central Dispatch are not available, so code using them is shown in plain (non-runnable) code blocks.
To run the examples yourself, use Xcode on a Mac (create a "Command Line Tool" project with the Objective-C language), or install clang and GNUstep on Linux.
Tip: You can still edit the code in the playgrounds and copy it into Xcode, where it compiles unchanged.
Test yourself
What is the relationship between C and Objective-C?
- Objective-C is a strict superset of C
- Objective-C is a subset of C
- They are unrelated languages
- Objective-C compiles to C++
Where does the NS prefix in names like NSString come from?
- Network Services
- NeXTSTEP
- New String
- Native Swift
Which language did Apple introduce in 2014 as the successor to Objective-C?
- Kotlin
- Rust
- Swift
- Go