Objective-C Reference

This section documents modern Objective-C as implemented by Apple Clang in the current Xcode release — Objective-C 2.0 plus ARC, literals and subscripting, instancetype, Clang modules, lightweight generics, nullability and @available — as published at Apple Developer Documentation and the Clang Objective-C specifications, which are the references these pages are written and verified against.

This content was generated with the assistance of AI and should be verified against developer.apple.com before being relied on in production.

This section’s bibliography lists the reference material consulted while preparing these pages.

Welcome to the Objective-C reference. Objective-C is C with Smalltalk-style object messaging added — a strict superset of C in which method calls are written [receiver message] and resolved by a run-time library at the moment they are sent, rather than by the compiler. Created by Brad Cox and Tom Love in the early 1980s, adopted by NeXT and then by Apple, it was the language of Cocoa for two decades and remains in service across Apple’s own frameworks, long-lived application codebases, and any project that needs the dynamism Swift deliberately does not offer. This section documents modern Objective-C as implemented by Apple Clang — Objective-C 2.0 plus ARC, literals and subscripting, instancetype, Clang modules, lightweight generics, nullability and @available — with GCC and GNUstep noted where Linux differs.

New here? Read in this order. Start with Getting Started, then the two C-level pages (Basic Syntax and Types and Operators, Control Flow and Functions) to fix the vocabulary. Next read Objects, classes and messaging straight through — it is the heart of the language, and everything later assumes it. Then read Automatic Reference Counting, since memory management shapes how APIs are designed. From there the remaining groups — Foundation values and collections, the runtime and dynamism, error handling, concurrency, interoperability, and code organisation and tooling — can be read in any order as you need them.

What’s covered

Getting started

  • Getting Started — what Objective-C is, its history from Cox and Love to the Swift era, how it compares with C++ and Swift, the Apple Clang/GCC/GNUstep toolchains, .h/.m/.mm, and "Hello, World".

  • Basic Syntax and Types — the @-directive table, BOOL/YES/NO, NSInteger/CGFloat, id/Class/SEL/IMP, instancetype, the nil/Nil/NULL/NSNull distinction, literals, NS_ENUM and NS_OPTIONS.

  • Operators, Control Flow and Functions — operators and BOOL truthiness, if/switch/loops, C functions versus Objective-C methods, pointers and structs, and the preprocessor (#import versus #include, #pragma mark, TARGET_OS_*).

Objects, classes and messaging

  • Classes and Objects — @interface/@implementation, ivars and visibility, @class, class versus instance methods, alloc/init and designated initializers, self and super, dealloc, NSObject, isa and description.

  • Messaging and Selectors — message-send syntax, messages to nil, selectors and respondsToSelector:, performSelector: and IMP, dynamic binding, __kindof, and run-time introspection.

  • Properties and Encapsulation — @property and its full attribute table, autosynthesis, @synthesize and @dynamic, backing ivars, dot syntax, KVC-compliant accessor naming, and the readonly-public/readwrite-private idiom.

  • Inheritance and Polymorphism — subclassing and super, abstract classes, the isEqual:/hash contract, NSCopying, class clusters, and when to subclass versus compose.

  • Categories and Extensions — categories, class extensions as the private interface, associated objects, the category pitfalls, and posing as removed legacy.

  • Protocols and Delegation — @protocol, @required/@optional, id<Protocol> typing, conformance checks, and the delegate and data-source patterns (and why delegates are weak).

  • Blocks — block syntax and typedef`s, completion handlers, capture and `__block, stack versus heap blocks, retain cycles and the weak/strong dance, and blocks versus C function pointers.

Foundation values and collections

  • Strings, Numbers and Values — NSString/NSMutableString, format specifiers, comparison, ranges and encodings; NSNumber and boxing; NSValue, NSData, NSDate, NSNull, NSURL; C strings; and toll-free bridging.

  • Collections and Fast Enumeration — NSArray/NSDictionary/NSSet and their mutable variants, literals and subscripting, copying semantics, for…in and block enumeration, sorting and filtering with NSPredicate, NSIndexSet, NSCache, and thread-safety.

  • Lightweight Generics and Nullability — parameterised collections, generic classes with covariant/contravariant, __kindof, NS_ASSUME_NONNULL_BEGIN/END and the nullability qualifiers, @available and API_AVAILABLE, and what each means on the Swift side.

Memory management

  • Manual Retain Release — the ownership model and the four rules, retain/release/autorelease, autorelease pools, MRR accessor patterns, and the classic bugs ARC was introduced to eliminate.

  • Automatic Reference Counting — what ARC does and does not do, the ownership qualifiers, method families, retain cycles and weak, dealloc under ARC, @autoreleasepool, bridging casts, and diagnosing leaks.

The runtime and dynamism

  • The Objective-C Runtime — objc_msgSend and the dispatch path, classes, metaclasses and isa, the runtime C API, method swizzling and when not to use it, introspection, type encodings, NSInvocation and NSProxy.

  • Dynamic Method Resolution and Forwarding — +resolveInstanceMethod: and @dynamic properties, -forwardingTargetForSelector:, -forwardInvocation:, doesNotRecognizeSelector:, and transparent proxies.

  • Key-Value Coding and Observing — KVC key paths, collection operators and validation; KVO registration, automatic versus manual notification, dependent keys and the mandatory observer removal; plus NSNotificationCenter.

Error handling

  • Errors and Exceptions — the NSError out-parameter pattern, domains, codes and userInfo, NS_ERROR_ENUM; exceptions and why they are for programmer errors only; assertions; and logging with NSLog and os_log.

Concurrency

  • Concurrency — threads and the main thread, @synchronized and NSLock, why atomic is not thread safety, Grand Central Dispatch (queues, groups, semaphores, dispatch_once, barriers, QoS), NSOperationQueue, NSRunLoop, and collection thread-safety.

Interoperability

  • xref:programming-languages/objective-c/objective-c-plus-plus-and-c-interop.adoc[Objective-C and C Interoperability] -- `.mm` files, C objects as ivars, lambdas versus blocks, ARC and C++ containers, calling plain C, extern "C" and header hygiene, Core Foundation bridging, and wrapping opaque C types.

  • Swift Interoperability — the bridging header and the generated -Swift.h, @objc/@objcMembers, NS_SWIFT_NAME, NS_REFINED_FOR_SWIFT, and how nullability, generics, NS_ENUM and error out-parameters are imported.

Code organisation and tooling

  • Modules, Frameworks and Code Organization — #import/#include/@import, Clang modules and module.modulemap, umbrella headers, static versus dynamic frameworks, header visibility, NS_UNAVAILABLE and NS_DESIGNATED_INITIALIZER, and class prefixes as namespaces.

  • Coding Conventions and Style — Apple’s Coding Guidelines for Cocoa (method, accessor, delegate, constant and notification naming), documentation comments and availability attributes, and the Cocoa design patterns.

  • Build and Tooling — Xcode and xcodebuild, the Clang flag reference, building with GCC/GNUstep, the static analyzer, sanitizers, Instruments, lldb, CocoaPods and Swift Package Manager, and clang-format.

  • Testing — XCTest, the assertion family, XCTestExpectation for asynchronous code, performance tests, mocking with OCMock, xcodebuild test, and unit testing on Linux with GNUstep.

Reference

Bibliography

The reference material consulted while preparing these pages.

Apple: language and core guides

Apple: the runtime

Apple: framework references

Books consulted

Consulted while preparing these pages; the pages themselves are written and verified against the primary sources above.