Your First Program
Every Objective-C command-line program has the same small skeleton. Let's build one and look at each part.
#import <Foundation/Foundation.h>
int main(void) {
@autoreleasepool {
NSLog(@"Hello, Objective-C!");
}
return 0;
}Hello, Objective-C!
Line by line
| Code | Meaning |
|---|---|
#import <Foundation/Foundation.h> |
Brings in the Foundation framework: strings, numbers, collections, NSLog and much more. #import is like C's #include but never includes the same file twice |
int main(void) |
The C entry point. The program starts here; void means it takes no command-line arguments |
@autoreleasepool { … } |
A scope that cleans up temporary objects when it ends (explained in the memory lessons). Wrap your code in one |
NSLog(@"…") |
Prints a message followed by a newline |
@"Hello, Objective-C!" |
A string literal that creates an NSString object. Note the @ in front: without it, "…" is a plain C string |
return 0; |
Tells the operating system the program succeeded |
Note: On a Mac,
NSLogalso prints the date, time and process name in front of each line, like2026-01-15 10:00:00.123 hello[812:9014] Hello. The outputs in this course leave that prefix out so you can focus on the message.
Format strings
NSLog works like C's printf: the first argument is a format string, and each % specifier is replaced by one of the following arguments.
#import <Foundation/Foundation.h>
int main(void) {
@autoreleasepool {
NSString *city = @"Lisbon";
int visitors = 42;
double rating = 4.75;
char grade = 'A';
NSLog(@"City: %@", city); // %@ = any object
NSLog(@"Visitors: %d", visitors); // %d = int
NSLog(@"Rating: %.1f", rating); // %f = double, .1 = one decimal
NSLog(@"Grade: %c", grade); // %c = char
NSLog(@"100%% done"); // %% = a literal percent sign
}
return 0;
}City: Lisbon
Visitors: 42
Rating: 4.8
Grade: A
100% done
The most common specifiers:
| Specifier | Type |
|---|---|
%@ |
any Objective-C object (prints its description) |
%d, %i |
int |
%ld |
long (use it for NSInteger with a (long) cast) |
%lu |
unsigned long (use it for NSUInteger with an (unsigned long) cast) |
%f, %.2f |
double / float |
%s |
a C string (char *) |
%c |
char |
%x |
an integer in hexadecimal |
NSInteger and the cast
Apple code uses NSInteger and NSUInteger instead of int: they are 64 bits wide on modern platforms. Their size differs between 32- and 64-bit systems, so the portable way to print them is to cast to long / unsigned long:
#import <Foundation/Foundation.h>
int main(void) {
@autoreleasepool {
NSInteger temperature = -5;
NSUInteger count = 12;
NSLog(@"Temperature: %ld", (long)temperature);
NSLog(@"Count: %lu", (unsigned long)count);
NSLog(@"Width and padding: [%5ld] [%-5ld] [%05ld]", (long)count, (long)count, (long)count);
}
return 0;
}Temperature: -5
Count: 12
Width and padding: [ 12] [12 ] [00012]
%5ld pads to 5 characters on the left, %-5ld pads on the right, and %05ld pads with zeros.
Common mistakes
- Forgetting the
@:NSLog("Hi")passes a C string where anNSStringis required, and the compiler complains. - The wrong specifier: printing an object with
%dor a number with%@prints garbage or crashes. Objects always use%@. - Missing semicolons: just as in C, every statement ends with
;.
Tip:
printffrom C also works, but it writes to a different stream thanNSLog(standard output instead of standard error), so mixing the two can print lines out of order. This course usesNSLogthroughout.
Comments
Objective-C uses C's comments: // until the end of the line, and /* … */ for blocks of text.
#import <Foundation/Foundation.h>
/*
* A program that prints a greeting.
* Block comments can span many lines.
*/
int main(void) {
@autoreleasepool {
// NSLog(@"This line is commented out");
NSLog(@"Only this line runs"); // a comment after code
}
return 0;
}Only this line runs
Test yourself
Which format specifier prints an Objective-C object such as an NSString?
- %s
- %d
- %@
- %o
What is the difference between "hi" and @"hi"?
- None
@"hi"is an NSString object,"hi"is a C string"hi"is an NSString object,@"hi"is a C string@"hi"is a character
What is the portable way to print an NSInteger named n?
- NSLog(@"%@", n)
- NSLog(@"%ld", (long)n)
- NSLog(@"%s", n)
- NSLog(@"%c", n)