Learn Objective-c - 10 Code Examples & CST Typing Practice Test
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to C. It has been the primary language for macOS and iOS development before Swift, enabling developers to create applications for Apple platforms with dynamic runtime capabilities.
View all 10 Objective-c code examples →
Learn OBJECTIVE-C with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
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).
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.
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.
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.
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.
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.
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.
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.
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.
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.
Frequently Asked Questions about Objective-c
What is Objective-c?
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to C. It has been the primary language for macOS and iOS development before Swift, enabling developers to create applications for Apple platforms with dynamic runtime capabilities.
What are the primary use cases for Objective-c?
macOS and iOS application development. Legacy Apple framework integration. Developing Cocoa and Cocoa Touch applications. Creating reusable Objective-C libraries. High-performance system-level Apple apps
What are the strengths of Objective-c?
Stable and mature language with long Apple ecosystem support. Full access to low-level C performance and system APIs. Dynamic runtime enables flexible behaviors. Large codebase of libraries and frameworks. Still supported in Xcode for iOS/macOS development
What are the limitations of Objective-c?
Verbosity and less readable syntax compared to modern Swift. Steeper learning curve for newcomers. Manual memory management pre-ARC can be error-prone. Slower adoption of modern language features. Declining community focus compared to Swift
How can I practice Objective-c typing speed?
CodeSpeedTest offers 10+ real Objective-c code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.