Getting Started
3 snippetsObjective-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 snippetsType 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 snippetsArithmetic 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.
Control Flow
3 snippetsConditional 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 snippetsIteration 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 snippetsInstance 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 snippetsObject-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 snippetsProperty 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