Featured image of post Objective-C in a Nutshell

Objective-C in a Nutshell

With Code Examples

Objective-C is an object-oriented programming language developed in the early 1980s.

It was created by Brad Cox and Tom Love and was later adopted by NeXT, the company founded by Steve Jobs after leaving Apple in 1985.

When Apple acquired NeXT in 1996, Objective-C became the primary language for macOS and iOS development.

A Brief History of Objective-C

Objective-C was designed as an extension of the C programming language, adding Smalltalk-style messaging to bring object-oriented capabilities.

It became the foundation of Apple’s software ecosystem, including macOS and iOS applications, until Swift emerged as its successor in 2014.

NeXT played a crucial role in popularizing Objective-C through its NeXTSTEP OS, which later evolved into macOS.

Steve Jobs’ vision for a modern computing platform heavily influenced Apple’s transition to macOS and iOS, making Objective-C’s popular for decades.

Also you will notice a lot of “NS” prefixes- Thats for NextStep..

So to keep backwards compatibility , with the NextStep code.. they held on to the NS prefix for quite awhile..

I guess the positive of this would be- out of the gate - when Mac OS was released- they could already leverage some of the Software Companies making NextStep apps, to make Mac OS apps..

It is a little weird though.. :)

1. Hello, World!

1
2
3
4
5
6
7
8
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"Hello, World!");
    }
    return 0;
}

The NSLog function prints text to the console, and @autoreleasepool manages memory for Objective-C objects.

2. Variables and Constants

1
2
3
NSString *name = @"Objective-C";
int age = 10;
age += 1;

Objective-C uses NSString * for string objects and standard C types for primitives.

3. Functions

1
2
3
NSString *greet(NSString *name) {
    return [NSString stringWithFormat:@"Hello, %@!", name];
}

Functions in Objective-C use the NSString class for string manipulation.

4. Pointers and Memory Management

1
2
3
NSString *message = [[NSString alloc] initWithFormat:@"Memory Management in Objective-C"];
NSLog(@"%@", message);
[message release];

Before Automatic Reference Counting (ARC), manual memory management required using alloc and release.

5. Conditional Expressions

1
2
int number = 10;
NSString *result = (number > 0) ? @"Positive" : @"Negative";

Objective-C supports ternary conditional expressions, similar to C.

6. Loops

1
2
3
for (int i = 1; i <= 5; i++) {
    NSLog(@"%d", i);
}

Objective-C supports traditional C-style loops for iteration.

7. Switch Statement

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
char grade = 'A';
switch (grade) {
    case 'A':
        NSLog(@"Excellent!");
        break;
    case 'B':
        NSLog(@"Good job!");
        break;
    default:
        NSLog(@"Keep trying!");
        break;
}

switch statements in Objective-C follow C syntax.

8. Classes and Objects

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@interface Person : NSObject
@property NSString *name;
@property int age;
@end

@implementation Person
@end

Person *person = [Person new];
person.name = @"Alice";
NSLog(@"%@", person.name);

Objective-C classes use @interface and @implementation keywords.

9. Categories (Extensions)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@interface NSString (Shouting)
- (NSString *)shout;
@end

@implementation NSString (Shouting)
- (NSString *)shout {
    return [self uppercaseString];
}
@end

NSLog(@"%@", [@"hello" shout]);

Categories allow adding methods to existing classes without modifying them.

10. Blocks (Closures)

1
2
3
4
void (^printMessage)(void) = ^{
    NSLog(@"Objective-C Blocks Example");
};
printMessage();

Blocks provide inline function expressions similar to Swift closures.


Key Ideas Table

Key IdeaSummary
Objective-C OverviewA C-based object-oriented language used for macOS and iOS development
HistoryDeveloped in the 1980s, adopted by NeXT, and later became Apple’s primary language
Hello WorldUses NSLog to print messages to the console
VariablesUses NSString * for strings and C types for primitives
FunctionsObjective-C functions use NSString for string handling
Memory ManagementUsed manual retain and release before ARC
ConditionalsSupports ternary operators and switch statements
ClassesUses @interface and @implementation for defining classes
CategoriesAllows extending existing classes without modification
BlocksProvides inline function expressions for callbacks and functional programming

References

  1. Apple’s Objective-C Guide
  2. History of Objective-C
  3. NeXT and Objective-C
  4. Steve Jobs and NeXT
  5. Objective-C Programming