Objective-C was developed by Brad Cox and Tom Love in the early 1980s at their company, Productivity Products International (PPI). Their goal was to add Smalltalk-style messaging and object-oriented programming structures to the C programming language.
In 1988, NeXT (founded by Steve Jobs) licensed Objective-C and developed the NeXTSTEP environment, which laid the foundation for the standard Cocoa libraries (Foundation and AppKit).
When Apple acquired NeXT in 1996, Objective-C became the primary programming language for OS X (now macOS) and later the iOS SDK for iPhones and iPads.
In 2014, Apple introduced Swift as a modern successor, though Objective-C remains highly active in legacy codebases, systems programming, and interoperability.
Who:
Brad Cox and Tom Love (creators), licensed and popularized by NeXT and Apple.
Why:
Developed to bridge the raw execution efficiency of C with the dynamic design, clean modularity, and messaging capabilities of Smalltalk.
Introduction
Advantages
Full C/C++ Interoperability — Objective-C is a strict superset of C. C and C++ code (via Objective-C++) can be compiled directly within ObjC files.
Dynamic Runtime Messaging — Message binding occurs at runtime rather than compile-time, enabling powerful reflection, method swizzling, and dynamic proxy behaviors.
Nil Messaging Safety — Sending messages to nil is safe and returns nil (or 0) without throwing exceptions, preventing a vast category of null pointer crashes.
Proven Stability — Backed by decades of production use on macOS and iOS, with highly optimized Cocoa libraries.
Disadvantages
Verbose and Quirky Syntax — The nested square bracket messaging syntax ([[receiver alloc] init]) and long method names can be hard to read for beginners.
Lack of Modern Safety Features — No built-in null-safety/optionals (added later as compiler annotations like _Nullable), namespaces, or strict value-type structures.
Manual Pointer Overhead — Everything is an object pointer (NSObject *), exposing developers to potential memory issues (retain cycles, weak dereferences).
Remember Points
Everything is a Pointer — Objective-C object variables are always pointers (e.g., NSString *name, not NSString name).
Square Brackets for Actions — Operations on objects are done by sending messages inside square brackets: [receiver message].
Dynamic Binding — The runtime evaluates the actual method implementation of a selector when the message is dispatched.
Basics
Hello World & Entry Point
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) { @autoreleasepool { // NSLog prints output with date/time stamps and trailing newline NSLog(@"Hello, World!"); } return 0;}
#import automatically prevents duplicate headers (no need for C-style #ifndef guards).
@autoreleasepool establishes a block for tracking temporary objects released via autorelease pools.
@"" declares an immutable constant NSString object literal.
Comment Syntax
// This is a single-line comment./* This is a multi-line comment block. */
Variables and String Literals
// C Primitives (stored on stack)int counter = 42;double price = 19.99;BOOL isActive = YES; // YES (1) or NO (0)// Objective-C Objects (stored on heap, always declared as pointers)NSString *str = @"VR Rathod";NSNumber *num = @100; -- Object literal wrapping an intNSArray *array = @[@"red", @"green", @"blue"]; -- Array literalNSDictionary *dict = @{@"key": @"value"}; -- Dictionary literal
Messaging Paradigm
Dynamic Message Passing
In Objective-C, you do not call functions on objects; you send messages to them. The runtime decides which method implementation is executed.
// C++ or Java style: receiver.doSomething(arg1, arg2);// Objective-C style:[receiver doSomethingWithArg1:val1 andArg2:val2];// Nested Messaging// Allocates memory on heap, then initializes the object stateNSString *newString = [[NSString alloc] initWithFormat:@"Count: %d", 5];
Classes: Interface vs. Implementation
The Interface (.h file)
Defines public fields, properties, and method declarations:
Property attributes dictate thread safety, memory ownership, and accessibility characteristics:
// 1. Thread Safety (atomic vs nonatomic)// atomic (Default) - Safely locks getters/setters (slower execution)// nonatomic - Not thread-safe, but executes much faster (highly recommended for UI elements)@property (nonatomic) int count;// 2. Memory Ownership (strong, weak, assign, copy)// strong (ARC default) - Increases retain reference count, keeping object alive// weak - Does not increase reference count. Set to nil if target is garbage collected (prevents dangling pointers)// assign - Simple value copy (used for C primitives)// copy - Duplicates the object (highly recommended for immutable classes like NSString, NSArray)@property (nonatomic, strong) MyModel *model;@property (nonatomic, weak) id delegate;@property (nonatomic, copy) NSString *title;// 3. Read/Write Permissions (readwrite vs readonly)@property (nonatomic, readonly) NSString *identifier;
Methods: Instance vs. Class
Declarations and Scope
// Instance Methods (-)// Must be executed on a constructed instance of the class- (void)calculateTotals;// Class Methods (+)// Executed directly on the class interface namespace (factory/helper methods)+ (instancetype)personWithName:(NSString *)name;// Usage:// Person *p = [Person personWithName:@"Alice"]; -- Class method// [p calculateTotals]; -- Instance method
// Modern ObjC uses ARC: the compiler automatically inserts retain/release calls at compile-time// Developers do not call [p release] manually.// Retain Cycles (Strong Reference Cycles)// Two objects retaining strong references to each other will leak memory:// Object A (strong) -> Object B// Object B (strong) -> Object A// Fix: Declare parent references as strong, and child/delegates as weak:@property (nonatomic, weak) ParentClass *parent;
Categories & Extensions
Categories
Categories allow adding new methods to existing classes without subclassing them:
// Declared at the top of .m files. Enables private properties/methods@interface Person ()@property (nonatomic, strong) NSString *privateToken; -- Private property- (void)executeSecretRoutine; -- Private method@end
Protocols (Interfaces)
Declaring & Implementing Protocols
Protocols define interface blueprints that classes can implement: