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, 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, thenil/Nil/NULL/NSNulldistinction, literals,NS_ENUMandNS_OPTIONS. -
Operators, Control Flow and Functions — operators and
BOOLtruthiness,if/switch/loops, C functions versus Objective-C methods, pointers and structs, and the preprocessor (#importversus#include,#pragma mark,TARGET_OS_*).
Objects, classes and messaging
-
Classes and Objects —
@interface/@implementation, ivars and visibility,@class, class versus instance methods,alloc/initand designated initializers,selfandsuper,dealloc,NSObject,isaanddescription. -
Messaging and Selectors — message-send syntax, messages to
nil, selectors andrespondsToSelector:,performSelector:andIMP, dynamic binding,__kindof, and run-time introspection. -
Properties and Encapsulation —
@propertyand its full attribute table, autosynthesis,@synthesizeand@dynamic, backing ivars, dot syntax, KVC-compliant accessor naming, and thereadonly-public/readwrite-private idiom. -
Inheritance and Polymorphism — subclassing and
super, abstract classes, theisEqual:/hashcontract,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 areweak). -
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;NSNumberand boxing;NSValue,NSData,NSDate,NSNull,NSURL; C strings; and toll-free bridging. -
Collections and Fast Enumeration —
NSArray/NSDictionary/NSSetand their mutable variants, literals and subscripting, copying semantics,for…inand block enumeration, sorting and filtering withNSPredicate,NSIndexSet,NSCache, and thread-safety. -
Lightweight Generics and Nullability — parameterised collections, generic classes with
covariant/contravariant,__kindof,NS_ASSUME_NONNULL_BEGIN/ENDand the nullability qualifiers,@availableandAPI_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,deallocunder ARC,@autoreleasepool, bridging casts, and diagnosing leaks.
The runtime and dynamism
-
The Objective-C Runtime —
objc_msgSendand the dispatch path, classes, metaclasses andisa, the runtime C API, method swizzling and when not to use it, introspection, type encodings,NSInvocationandNSProxy. -
Dynamic Method Resolution and Forwarding —
+resolveInstanceMethod:and@dynamicproperties,-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
NSErrorout-parameter pattern, domains, codes anduserInfo,NS_ERROR_ENUM; exceptions and why they are for programmer errors only; assertions; and logging withNSLogandos_log.
Concurrency
-
Concurrency — threads and the main thread,
@synchronizedandNSLock, whyatomicis 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_ENUMand error out-parameters are imported.
Code organisation and tooling
-
Modules, Frameworks and Code Organization —
#import/#include/@import, Clang modules andmodule.modulemap, umbrella headers, static versus dynamic frameworks, header visibility,NS_UNAVAILABLEandNS_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, andclang-format. -
Testing — XCTest, the assertion family,
XCTestExpectationfor asynchronous code, performance tests, mocking with OCMock,xcodebuild test, and unit testing on Linux with GNUstep.
Reference
-
Cheat Sheet (PDF) — a single-page, printable summary of the whole section.
Bibliography
The reference material consulted while preparing these pages.
Apple: language and core guides
-
Programming with Objective-C — Apple’s primary language guide.
-
The Objective-C Programming Language — the older, more formal language description.
-
Cocoa Encyclopedia — class clusters, object mutability, toll-free bridging.
Apple: the runtime
-
Objective-C Runtime — the current API reference.
-
objc4— the runtime’s source, the authority of last resort.
Clang specifications
-
Automatic Reference Counting — the normative ARC specification.
Books consulted
Consulted while preparing these pages; the pages themselves are written and verified against the primary sources above.
-
Uzayr, Sufyan bin. Objective-C: The Ultimate Guide. Boca Raton: CRC Press / Taylor & Francis, 2024. ISBN 9781032413198.
doi:10.1201/9781003357506— publisher’s book page. -
Uzayr, Sufyan bin. Mastering Objective-C: A Beginner’s Guide (Mastering Computer Science). Boca Raton: CRC Press / Taylor & Francis, 2024. ISBN 9781032415284.
doi:10.1201/9781003358534— publisher’s book page.