Objective-C
- Pronunciation
- ob-JEK-tiv SEE
In short
Objective-C is an object-oriented superset of C with Smalltalk-style messaging, and it was the main language for macOS and iOS apps before Swift arrived.
What is Objective-C?
Objective-C is a general-purpose, object-oriented programming language created by Brad Cox and Tom Love in the early 1980s. It adds objects and message passing, inspired by the Smalltalk language, on top of C, and any valid C code is also valid Objective-C. NeXT adopted it in the late 1980s, and after Apple acquired NeXT, it became the foundation of development for macOS and later iOS.
Its most visible feature is message syntax in square brackets: [greeter sayHelloTo:@"Ada"] sends the message sayHelloTo: to the object greeter. Messages are dispatched at runtime, which makes the language very dynamic: the method that runs is looked up while the program is running, and categories can add methods to existing classes, even ones from system libraries. Since 2011, memory has been managed with Automatic Reference Counting (ARC), which inserts the retain and release calls that developers once had to write by hand.
Objective-C was used to build Apple's Cocoa and Cocoa Touch frameworks, and a large amount of existing app and system code is still written in it. New Apple apps are usually written in Swift, but Objective-C remains important for maintaining older codebases and for mixing with C and C++ libraries, including through the variant called Objective-C++.
Objective-C is most often compared with Swift, its successor on Apple platforms. Swift has a cleaner syntax, stronger static type safety, optionals that make missing values explicit and value types like structs, while Objective-C is more dynamic and treats sending a message to nil as a harmless no-op instead of an error. The two interoperate, so one app can contain both. Objective-C is also different from C++, which added object orientation to C in a completely different, compile-time-focused way.
Key takeaways
- Objective-C is a strict superset of C with Smalltalk-style objects and messaging.
- Method calls are messages written in square brackets and dispatched at runtime.
- It was the main language for macOS and iOS development before Swift.
- Memory is managed with Automatic Reference Counting (ARC).
- Objective-C and Swift can be mixed in the same project.
Example
#import <Foundation/Foundation.h>
int main(void) {
@autoreleasepool {
// Square brackets send a message to an object
NSArray *langs = @[@"C", @"Smalltalk", @"Objective-C"];
NSString *joined = [langs componentsJoinedByString:@" + "];
NSLog(@"%@", joined); // C + Smalltalk + Objective-C
// Messages can be nested, and a message sent to nil is ignored
NSString *nothing = nil;
NSLog(@"%@ %lu", [joined uppercaseString], [nothing length]); // C + SMALLTALK + OBJECTIVE-C 0
}
return 0;
}Readers ask
Is Objective-C still used?
Yes, mainly in existing Apple codebases and system frameworks. Most new iOS and macOS code is written in Swift, but Objective-C remains supported and can be mixed with Swift in the same app.
What is the difference between Objective-C and Swift?
Objective-C is a dynamic superset of C with bracket-based messaging, while Swift is a newer language with stronger type safety, optionals, value types and a cleaner syntax. Apple recommends Swift for new code, and the two interoperate.
What is the difference between Objective-C and C++?
Both extend C with object-oriented features, but in different ways. Objective-C adds dynamic, runtime message passing inspired by Smalltalk, while C++ adds classes, templates and compile-time features, and the two can be combined in Objective-C++.
See also
- SwiftProgramming Languages, p. 31Swift is a compiled, statically typed language designed for safety and speed, used mainly to build apps for iPhone, iPad, Mac, and other Apple platforms.
- CProgramming Languages, p. 3C is a compiled, low-level programming language that gives direct control over memory and is used to build operating systems, drivers, and embedded software.
- C++Programming Languages, p. 5C++ is a compiled, statically typed language that extends C with classes and templates while keeping low-level control over memory and performance.
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
- Garbage CollectionProgramming Fundamentals, p. 23Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
- NullProgramming Fundamentals, p. 38Null is a special value that means "no value here", used when a variable deliberately refers to no object or data at all instead of holding something real.
Spotted a mistake or something missing on this page?Suggest an edit