AI Generate Objective-C docs instantly

Objective-C Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

3 snippets

Objective-C basics

Hello World

#import <Foundation/Foundation.h>

int main() {
    @autoreleasepool {
        NSLog(@"Hello, World!");
    }
    return 0;
}

Import Headers

#import <Foundation/Foundation.h>
#import "MyClass.h"

Comments

// Single line comment

/*
 * Multi-line
 * comment
 */

Variables & Data Types

6 snippets

Type declarations

Primitives

int age = 30;
float price = 19.99f;
double pi = 3.14159;
BOOL flag = YES;  // YES or NO
char letter = 'A';

NSString

NSString *name = @"John";
NSString *formatted = [NSString stringWithFormat:@"Age: %d", age];

NSNumber

NSNumber *count = @42;
NSNumber *price = @19.99;
NSNumber *flag = @YES;

NSArray (Immutable)

NSArray *fruits = @[@"Apple", @"Banana", @"Orange"];
NSString *first = fruits[0];

NSMutableArray

NSMutableArray *list = [[NSMutableArray alloc] init];
[list addObject:@"Item"];
[list removeObjectAtIndex:0];

NSDictionary

NSDictionary *person = @{
    @"name": @"John",
    @"age": @30
};
NSString *name = person[@"name"];

Operators

4 snippets

Arithmetic and logical operations

Arithmetic

int sum = a + b;
int diff = a - b;
int prod = a * b;
int quot = a / b;
int mod = a % b;

Comparison

if (a == b) { }
if (a != b) { }
if (a > b) { }
if (a < b) { }
if (a >= b) { }
if (a <= b) { }

Logical

if (a && b) { }  // AND
if (a || b) { }  // OR
if (!flag) { }   // NOT

String Comparison

if ([str1 isEqualToString:str2]) { }
if ([str compare:@"test"] == NSOrderedSame) { }

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Control Flow

3 snippets

Conditional statements

If Statement

if (age >= 18) {
    NSLog(@"Adult");
} else {
    NSLog(@"Minor");
}

Ternary

NSString *result = (age >= 18) ? @"Adult" : @"Minor";

Switch

switch (dayOfWeek) {
    case 1:
        NSLog(@"Monday");
        break;
    case 2:
        NSLog(@"Tuesday");
        break;
    default:
        NSLog(@"Other");
        break;
}

Loops

5 snippets

Iteration constructs

For Loop

for (int i = 0; i < 10; i++) {
    NSLog(@"%d", i);
}

For-In (Fast Enumeration)

NSArray *fruits = @[@"Apple", @"Banana"];
for (NSString *fruit in fruits) {
    NSLog(@"%@", fruit);
}

While Loop

int i = 0;
while (i < 10) {
    NSLog(@"%d", i);
    i++;
}

Do-While

int i = 0;
do {
    NSLog(@"%d", i);
    i++;
} while (i < 10);

Block Enumeration

[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@"Index %lu: %@", idx, obj);
}];

Methods & Blocks

5 snippets

Instance and class methods

Instance Method

- (void)greet:(NSString *)name {
    NSLog(@"Hello, %@", name);
}

// Call
[self greet:@"Alice"];

Method with Return

- (NSInteger)addA:(NSInteger)a toB:(NSInteger)b {
    return a + b;
}

// Call
NSInteger sum = [self addA:5 toB:3];

Class Method

+ (instancetype)createWithName:(NSString *)name {
    return [[self alloc] initWithName:name];
}

// Call
MyClass *obj = [MyClass createWithName:@"Test"];

Blocks (Closures)

void (^greetBlock)(NSString *) = ^(NSString *name) {
    NSLog(@"Hello, %@", name);
};

greetBlock(@"Alice");

Block as Parameter

- (void)performWithCompletion:(void (^)(BOOL success))completion {
    // Do work
    completion(YES);
}

Classes & Objects

4 snippets

Object-oriented programming

Class Interface (.h)

@interface Person : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;

- (void)greet;

@end

Class Implementation (.m)

@implementation Person

- (void)greet {
    NSLog(@"Hello, I'm %@", self.name);
}

@end

Initialize Object

Person *person = [[Person alloc] init];
person.name = @"John";
person.age = 30;
[person greet];

Custom Initializer

- (instancetype)initWithName:(NSString *)name age:(NSInteger)age {
    self = [super init];
    if (self) {
        _name = name;
        _age = age;
    }
    return self;
}

Properties & Memory

3 snippets

Property attributes and ARC

Property Attributes

@property (nonatomic, strong) NSString *name;
@property (nonatomic, weak) id<Delegate> delegate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) NSInteger count;
@property (nonatomic, readonly) NSDate *createdAt;

Strong vs Weak

@property (strong) NSObject *strongRef;  // Retained
@property (weak) NSObject *weakRef;      // Not retained

ARC (Automatic Reference Counting)

// ARC manages memory automatically
Person *p = [[Person alloc] init];  // Retained
// Released automatically when out of scope

More Cheat Sheets

FAQ

Frequently asked questions

What is a Objective-C cheat sheet?

A Objective-C cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in Objective-C. It helps developers quickly look up syntax without searching through documentation.

How do I learn Objective-C quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important Objective-C concepts?

Key Objective-C concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my Objective-C code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your Objective-C code using AI.

Code Conversion Tools

Convert Objective-C to Other Languages

Easily translate your Objective-C code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate Objective-C Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week