typestar

@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

  1. @interface Record : NSObject declares the class and its parent.
  2. Signatures live in the interface; bodies in the implementation.
  3. - (NSString *)describe is an instance method returning a string.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Classes, step 7 of 27 in Language basics.

← Previous Next →