AnatomyOf

Anatomy of an Objective-C file

example.m
1#import <Foundation/Foundation.h> // no ceremony spared
2
3@interface Robot : NSObject
4@property (nonatomic, strong) NSString *name;
5- (void)announceSelf;
6@end
7
8@implementation Robot
9- (void)announceSelf {
10 NSLog(@"Beep boop, I am %@.", self.name); // beep boop is load-bearing
11}
12@end
13
14int main(int argc, const char *argv[]) {
15 @autoreleasepool {
16 Robot *unit = [Robot new];
17 unit.name = @"Bender";
18 [unit announceSelf]; // one bracket to rule them all
19 }
20 return 0;
21}
Objective-C is a superset of C that adds Smalltalk-style messaging, used extensively for Apple's macOS and iOS development before Swift.