๐Ÿ“ฑ

Objective-C Cheat Sheet

The language that built iOS and macOS for nearly two decades before Swift arrived โ€” a C superset with Smalltalk-style "message passing" bolted on. Apple has moved new development to Swift, but Objective-C is still deeply embedded in legacy Apple codebases and many older app SDKs.

๐Ÿ“ Reference page only โ€” Objective-C's whole point is deep integration with Apple's Cocoa/Cocoa Touch frameworks, which require a real macOS + Xcode environment to run meaningfully; there's no safe free browser sandbox for that. Xcode (free from Apple, macOS only) is the standard way to actually build and run these examples.
Jump to: Your first programInterfaces & implementationSending messages Propertiesif / elseLoops

Your first program

#import <Foundation/Foundation.h>

int main() {
    NSLog(@"Hello, world!");
    return 0;
}
๐Ÿ“ The @ prefix marks Objective-C-specific syntax (like @"..." string literals) layered on top of plain C.

Interfaces & implementation

Every class is split into two blocks โ€” @interface declares what it looks like, @implementation defines what it does.

@interface Dog : NSObject
@property (strong, nonatomic) NSString *name;
@property (nonatomic) int age;
- (void)bark;
@end

@implementation Dog
- (void)bark {
    NSLog(@"%@ says woof!", self.name);
}
@end

Sending messages

Objective-C's signature syntax โ€” square brackets instead of dot notation for calling methods.

Dog *d = [[Dog alloc] init];
d.name = @"Rex";
[d bark];   // "sending the bark message to d"

Properties

@property (strong, nonatomic) NSString *name;   // auto-generates getter/setter
d.name = @"Rex";      // really calls [d setName:@"Rex"]
NSLog(@"%@", d.name);   // really calls [d name]

if / else

if (age >= 18) {
    NSLog(@"Adult");
} else if (age >= 13) {
    NSLog(@"Teen");
} else {
    NSLog(@"Child");
}

Loops

NSArray *nums = @[@10, @20, @30];
for (NSNumber *n in nums) {
    NSLog(@"%@", n);
}
โ†’ See the modern Swift cheat sheet