Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 38 ยท Getting Started

What Is Objective-C?

Learn what Objective-C is, where it came from, how messaging differs from function calls, and why it still matters for Apple platforms and legacy code bases.

Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C language. Every valid C program is also a valid Objective-C program, and on top of C you get classes, objects, dynamic dispatch, protocols, categories and blocks. For roughly thirty years it was the main language for writing software for NeXTSTEP, macOS and iOS.

Today most new Apple projects start in Swift, but Objective-C has not gone away. Huge amounts of production code, system frameworks, open-source libraries and game engines still use it, and Swift talks to it every day. Reading and writing Objective-C is a practical skill if you maintain an older app, work with Apple frameworks at a low level, or simply want to understand how a dynamic object system is built on top of plain C.

A short history

Objective-C was created in the early 1980s by Brad Cox and Tom Love. They wanted the speed and portability of C together with the productive, dynamic style of Smalltalk. NeXT licensed the language in 1988 and built its operating system and the Foundation and AppKit frameworks in it. Apple acquired NeXT in 1997, and NeXTSTEP became the base of Mac OS X. The iPhone SDK (2008) made Objective-C one of the most widely used languages in the world.

Important milestones since then:

  • Objective-C 2.0 (2007) added properties, fast enumeration and optional garbage collection.
  • Automatic Reference Counting, ARC (2011) made the compiler insert memory-management calls for you. Garbage collection was dropped.
  • Blocks, literals and modules (2010 to 2012) brought closures, @[], @{} and @import.
  • Swift (2014) arrived as the newer language, designed to interoperate with Objective-C code.

What makes it different

Three ideas define the language.

It is a strict superset of C. int, for, pointers, struct, the preprocessor and the entire C standard library are all there. Objective-C adds new syntax in a clearly separate style: everything new starts with @ (such as @interface, @"text" or @selector) or uses square brackets for messages.

Objects receive messages. In C you call a function by name, and the linker decides which code runs. In Objective-C you send a message to an object, and at run time the object decides what to do with it:

OBJECTIVEC
[dog speak];                      // send the message "speak" to dog
[dog setName:@"Rex"];             // message with one argument
[dog walkFor:30 atSpeed:2.5];     // message with two arguments

The runtime is dynamic. Which method runs is looked up while the program executes. You can ask an object whether it responds to a message, add methods to existing classes, or swap implementations. That flexibility is what makes frameworks like Cocoa so adaptable.

Because everything not covered by C is handled by a small run-time library, Objective-C objects are always pointers to heap memory. You will see NSString *name, never NSString name.

The layers of the language

flowchart TB
  A["Your application code"] --> B["Cocoa / Foundation / AppKit / UIKit"]
  B --> C["Objective-C language: classes, messages, protocols, blocks"]
  C --> D["Objective-C runtime library (objc_msgSend, classes, selectors)"]
  D --> E["C language and C standard library"]

Each layer is built from the one below it. Objective-C itself is a thin layer over C.

How a message works

Square brackets look like a function call, but the compiler turns each one into a call to a run-time function, objc_msgSend, passing the receiver, a selector (the method name as a unique value) and the arguments. The runtime looks the selector up in the receiver's class, finds the implementation (a function pointer), and jumps to it.

Objective-C itself cannot run on this site, so the next example is a stand-in written in C that models the idea: a table maps selector names to function pointers, and "sending a message" means looking the name up at run time. It is not real Objective-C, but it is very close to what the runtime does.

C Stand-in (C): a message is a table lookup

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The last line mirrors what real Objective-C does when an object gets a message it cannot handle: the program reports an "unrecognized selector". The decision happens at run time, not compile time.

What an object looks like in memory

In the runtime, every object begins with a pointer called isa that points at its class. The class knows its name, its superclass and its method table. Here is a C model of that layout:

C Stand-in (C): objects start with a class pointer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

When you later learn about isKindOfClass:, inheritance and method resolution, this picture of a chain of classes is exactly what the runtime walks.

Strengths and weaknesses

Strengths: full access to C and C++ code (Objective-C++ even mixes both), direct access to every Apple framework, a very dynamic runtime, mature tooling, and a stable ABI.

Weaknesses: verbose method names, no namespaces (so classes get two or three letter prefixes such as NS or UI), pointer-heavy code, manual care around nil, and most new learning material and Apple features now target Swift.

Where you will meet it

  • Existing iOS and macOS apps and SDKs written before Swift.
  • Cocoa Touch and AppKit headers, which are still written in Objective-C and imported into Swift.
  • Cross-platform code via GNUstep, which implements Foundation on Linux and Windows.
  • Interview questions and technical deep dives about runtime behaviour.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How is Objective-C related to C?

    Objective-C adds object features on top of C. All C syntax, types and libraries are available.
  2. 2. What does the code [dog speak] do?

    Square brackets send a message. The method that runs is looked up dynamically by the Objective-C runtime.
  3. 3. What is the isa pointer in an object?

    isa points to the class object, which holds the method table and the link to the superclass.
  4. 4. Which sign marks most Objective-C additions to C, such as @interface?

    New keywords and literals start with @, which keeps them separate from C syntax.

Summary

  • Objective-C is C plus Smalltalk-style messaging, classes, protocols and blocks.
  • Square brackets send messages; the runtime picks the method while the program runs.
  • Objects are always pointers; each begins with an isa pointer to its class.
  • Swift has replaced it for most new projects, but Objective-C remains common in existing code and frameworks.
  • Because it is a superset of C, everything you know about C carries over.

Next: Setting Up and Your First Program