Message sends in Objective-C
Square brackets everywhere — and nil quietly absorbs messages.
#import <Foundation/Foundation.h>
int main(void) {
@autoreleasepool {
// Everything happens by sending messages in square brackets.
NSString *title = @"blue train";
// Sends nest inside sends, reading inside out.
NSString *display = [[title uppercaseString]
stringByAppendingString:@" [remastered]"];
NSLog(@"%@", display);
// A message to nil quietly answers zero -- no crash.
NSString *missing = nil;
NSLog(@"Missing length: %lu", (unsigned long)[missing length]);
}
return 0;
}
How it works
[title uppercaseString]sends a message to an object.- Sends nest: the inner result receives the outer message.
- Messaging nil returns zero instead of crashing — a defining quirk.
Keywords and builtins used here
NSStringintlongmainnilreturnunsignedvoid
The run, in numbers
- Lines
- 18
- Characters to type
- 483
- Tokens
- 75
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 69 seconds.
Step 1 of 3 in Messages, step 4 of 27 in Language basics.