Objective-C

On this page 7

Objective-C is a strict superset of the C language that adds object-oriented programming through dynamic message passing, a model borrowed from Smalltalk. Among programming languages it holds a distinctive place: it was the primary language for NeXTSTEP, and later for macOS and iOS application development, until Swift appeared in 2014.

Programming in Objective-C means combining ordinary C code with classes, objects and messages resolved at run time rather than at compile time. That runtime flexibility powers Apple frameworks such as Foundation and AppKit, and it explains why large amounts of production code on Apple platforms are still written in the language today.

How Objective-C message passing works

The defining mechanism of Objective-C is dynamic dispatch through messages. An expression such as [account deposit:100] does not call a fixed function address; the compiler turns it into a call to objc_msgSend, which looks up the selector in the receiver’s class at run time and jumps to the matching implementation.

The runtime caches these lookups, so a typical message send costs only a few nanoseconds on modern hardware, slightly more than a C++ virtual call. Because resolution is late, programs can add or replace methods while running, forward unknown messages to another object, and inspect classes through runtime introspection. Sending a message to nil is legal and simply returns zero, a convention that removes many null checks but can also hide bugs.

The table below lists the core language elements that Objective-C adds on top of C.

ElementSyntax examplePurpose
Class interface@interface Account : NSObjectDeclares state and methods
Message send[obj doWork]Dynamic method invocation
Selector@selector(doWork)Names a method at run time
Protocol@protocol DrawableDeclares a set of required methods
Category@interface NSString (Trim)Adds methods to an existing class
Block^(int x){ return x * 2; }Closure capturing surrounding values
Core Objective-C language elements

Memory management in Objective-C

Objective-C manages object lifetimes with reference counting: each object tracks how many owners hold it and is freed when that count reaches zero. Historically developers wrote retain and release calls by hand, a model called manual retain-release, which was a common source of leaks and crashes.

Since 2011 Automatic Reference Counting (ARC) has inserted those calls at compile time. ARC is deterministic and has no garbage collection pauses, but it cannot break cycles on its own, so developers mark back-references as weak to avoid retain cycles, especially with blocks that capture self.

ModelHow it worksMain weakness
Manual retain-releaseDeveloper calls retain and releaseLeaks and over-releases
Garbage collectionTracing collector, macOS only, now removedPauses and poor C interoperation
Automatic Reference CountingCompiler inserts reference count callsRetain cycles need weak references
Memory management models over time

Objective-C compared with C++ and Swift

Objective-C, C++ and Swift all extend or replace C for application work, but they make opposite choices about binding time and safety. C++ resolves most calls at compile time for maximum performance, Objective-C defers them to run time for flexibility, and Swift favors static typing with strong safety guarantees while still interoperating with the Objective-C runtime on Apple platforms.

AspectObjective-CC++Swift
Method bindingDynamic messagesMostly staticMostly static
Null handlingMessages to nil are safeUndefined behaviorOptionals enforced by compiler
Memory modelARCManual or smart pointersARC
C compatibilityFull supersetLargely compatibleThrough bridging
GenericsLightweight, compile-time hintsTemplatesFull generics
Comparison with related languages

Developers can also mix C++ and Objective-C in the same file using Objective-C++, with the .mm extension. This is common in game engines, audio software and cross-platform libraries that need a native Apple layer over a shared C++ core.

Where Objective-C is used today

Objective-C remains relevant mainly through existing codebases and framework internals. New Apple projects usually start in Swift, but many established applications, system frameworks and third-party SDKs still contain substantial Objective-C, and Swift code calls into it constantly through automatic bridging.

SituationRecommendationReason
Maintaining a large legacy appKeep Objective-C, migrate graduallyRewrites carry high risk
Wrapping C or C++ librariesUse Objective-C or Objective-C++Direct C interoperation
Heavy runtime manipulationObjective-CSwizzling and introspection are native
New iOS or macOS appSwiftBetter safety and tooling
Non-Apple platformsUsually another languageLimited framework support
When Objective-C is still a reasonable choice

Outside Apple, the language is compiled by Clang and GCC, and the open-source GNUstep project reimplements much of the Foundation and AppKit APIs. In practice, however, the value of Objective-C is inseparable from the Cocoa and Cocoa Touch frameworks, so cross-platform use remains rare.

Interoperation with Swift depends on annotations in Objective-C headers. Nullability qualifiers such as nonnull and nullable, together with lightweight generics like NSArray<NSString *>, let the Swift compiler import Objective-C APIs as precise, safe types instead of implicitly unwrapped optionals and untyped collections.

Limitations and common pitfalls

The flexibility of Objective-C comes with weaker compile-time guarantees. Many errors that Swift catches at build time surface only when a message reaches an object that cannot handle it, producing the familiar unrecognized selector crash.

  • Verbose syntax with long method names and header files slows development.
  • No namespaces, so classes use two- or three-letter prefixes such as NS or UI to avoid collisions.
  • Method swizzling is powerful but makes behavior hard to trace and can break after framework updates.
  • Silent nil messaging can hide logic errors that would otherwise fail fast.
  • Shrinking talent pool, because most new developers learn Swift first.

The simple explanation

Objective-C is a programming language that Apple used for decades to build apps for the Mac and the iPhone. It starts with the older C language and adds a way for pieces of a program, called objects, to send messages to each other.

Think of it as an office where staff pass notes instead of following a fixed script. Each note is read and handled when it arrives, which makes the system very flexible but less predictable. Today Apple recommends the newer Swift language, but many familiar apps still run on Objective-C code underneath.

How it is used in practice

A mobile team maintains a banking app first released in 2012, with roughly 300,000 lines of Objective-C. Rewriting it in Swift at once would take more than a year and risk regressions in payment features, so the team adopts gradual migration.

New screens are written in Swift, while the proven networking and encryption layers stay in Objective-C and are exposed through a bridging header. A small Objective-C++ module continues to wrap a shared C++ fraud-detection library also used by the Android app.

After three years about 60 percent of the code is Swift, crash rates have fallen because of stricter typing, and the app shipped every release on schedule. The remaining Objective-C is stable, well tested code that the team migrates only when a feature requires changes.