The language that built iOS and macOS for nearly two decades before Swift arrived โ a C superset with Smalltalk-style "message passing" bolted on. Apple has moved new development to Swift, but Objective-C is still deeply embedded in legacy Apple codebases and many older app SDKs.
#import <Foundation/Foundation.h>
int main() {
NSLog(@"Hello, world!");
return 0;
}
@ prefix marks Objective-C-specific syntax (like @"..." string literals) layered on top of plain C.Every class is split into two blocks โ @interface declares what it looks like, @implementation defines what it does.
@interface Dog : NSObject
@property (strong, nonatomic) NSString *name;
@property (nonatomic) int age;
- (void)bark;
@end
@implementation Dog
- (void)bark {
NSLog(@"%@ says woof!", self.name);
}
@end
Objective-C's signature syntax โ square brackets instead of dot notation for calling methods.
Dog *d = [[Dog alloc] init];
d.name = @"Rex";
[d bark]; // "sending the bark message to d"
@property (strong, nonatomic) NSString *name; // auto-generates getter/setter
d.name = @"Rex"; // really calls [d setName:@"Rex"]
NSLog(@"%@", d.name); // really calls [d name]
if (age >= 18) {
NSLog(@"Adult");
} else if (age >= 13) {
NSLog(@"Teen");
} else {
NSLog(@"Child");
}
NSArray *nums = @[@10, @20, @30];
for (NSNumber *n in nums) {
NSLog(@"%@", n);
}