@interface and @implementation in Objective-C
The contract and the code, declared separately.
#import <Foundation/Foundation.h>
// The contract in @interface, the code in @implementation.
@interface Record : NSObject
- (NSString *)describe;
@end
@implementation Record
- (NSString *)describe {
return @"a 12-inch LP, 33 rpm";
}
@end
int main(void) {
@autoreleasepool {
Record *lp = [[Record alloc] init];
NSLog(@"%@", [lp describe]);
}
return 0;
}
How it works
@interface Record : NSObjectdeclares the class and its parent.- Signatures live in the interface; bodies in the implementation.
- (NSString *)describeis an instance method returning a string.
Keywords and builtins used here
NSObjectNSStringRecorddescribeintreturnvoid
The run, in numbers
- Lines
- 20
- Characters to type
- 356
- Tokens
- 70
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 65 seconds.
Step 1 of 3 in Classes, step 7 of 27 in Language basics.