Learn OBJECTIVE-C with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
1
Objective-C Counter and Theme Toggle
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
int count = 0;
BOOL isDark = NO;
void (^updateUI)(void) = ^{
NSLog(@"Counter: %d", count);
NSLog(@"Theme: %@", isDark ? @"Dark" : @"Light");
};
void (^increment)(void) = ^{ count++; updateUI(); };
void (^decrement)(void) = ^{ count--; updateUI(); };
void (^reset)(void) = ^{ count = 0; updateUI(); };
void (^toggleTheme)(void) = ^{ isDark = !isDark; updateUI(); };
// Simulate actions
updateUI();
increment();
increment();
toggleTheme();
decrement();
reset();
}
return 0;
}
Demonstrates a simple counter with theme toggling using Objective-C console output (can be adapted for iOS apps).
2
Objective-C Random Number Generator
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
for (int i = 1; i <= 3; i++) {
int num = arc4random_uniform(100) + 1;
NSLog(@"Random %d: %d", i, num);
}
}
return 0;
}
Generates random numbers between 1 and 100 and prints them.
3
Objective-C Todo List
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *todos = [NSMutableArray array];
void (^addTask)(NSString *) = ^(NSString *task){ [todos addObject:task]; NSLog(@"%@", todos); };
void (^removeTask)(NSInteger) = ^(NSInteger index){ [todos removeObjectAtIndex:index]; NSLog(@"%@", todos); };
// Simulate actions
addTask(@"Buy milk");
addTask(@"Write Objective-C code");
removeTask(0);
}
return 0;
}
Maintains a simple todo list with add and remove functionality.
4
Objective-C Dice Roller
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
for (int i = 1; i <= 3; i++) {
int roll = arc4random_uniform(6) + 1;
NSLog(@"Roll %d: %d", i, roll);
}
}
return 0;
}
Rolls a six-sided dice three times.
5
Objective-C Countdown Timer
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
int count = 5;
while(count >= 0) {
NSLog(@"Countdown: %d", count);
count--;
}
NSLog(@"Done!");
}
return 0;
}
Counts down from 5 to 0.
6
Objective-C Prime Checker
#import <Foundation/Foundation.h>
BOOL isPrime(int n){
if(n<2) return NO;
for(int i=2;i*i<=n;i++){
if(n%i==0) return NO;
}
return YES;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
int nums[] = {7,10,13};
for(int i=0;i<3;i++){
NSLog(@"%d is %@", nums[i], isPrime(nums[i])?@"Prime":@"Not Prime");
}
}
return 0;
}
Checks if numbers are prime.
7
Objective-C Temperature Converter
#import <Foundation/Foundation.h>
float cToF(float c){ return c*9/5 +32; }
float fToC(float f){ return (f-32)*5/9; }
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"25°C = %.1f°F", cToF(25));
NSLog(@"77°F = %.1f°C", fToC(77));
}
return 0;
}
Converts Celsius to Fahrenheit and vice versa.
8
Objective-C Shopping Cart
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *cart = [NSMutableArray array];
NSMutableArray *prices = [NSMutableArray array];
void (^addItem)(NSString*, NSNumber*) = ^(NSString *item, NSNumber *price){
[cart addObject:item];
[prices addObject:price];
NSLog(@"Cart: %@", cart);
NSLog(@"Total: %@", [prices valueForKeyPath:@"@sum.self"]);
};
void (^removeItem)(NSInteger) = ^(NSInteger index){
[cart removeObjectAtIndex:index];
[prices removeObjectAtIndex:index];
NSLog(@"Cart: %@", cart);
NSLog(@"Total: %@", [prices valueForKeyPath:@"@sum.self"]);
};
// Simulate actions
addItem(@"Apple", @2);
addItem(@"Banana", @3);
removeItem(0);
}
return 0;
}
Adds and removes items in a shopping cart with total cost.
9
Objective-C Name Greeting
#import <Foundation/Foundation.h>
void greet(NSString *name){
NSLog(@"Hello, %@! Welcome!", name);
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
greet(@"Saurav");
greet(@"Alice");
greet(@"Bob");
}
return 0;
}
Greets users by name.
10
Objective-C Stopwatch
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
int time = 0;
while(time < 5){
NSLog(@"Stopwatch: %d seconds", time);
time++;
}
NSLog(@"Done!");
}
return 0;
}
Simulates a stopwatch incrementing seconds.