typestar

Properties in Objective-C

@property writes the getter, setter and storage for you.

#import <Foundation/Foundation.h>

// @property writes the getter, setter and storage for you.
@interface Album : NSObject
@property (copy) NSString *title;
@property NSInteger year;
@end

@implementation Album
@end
int main(void) {
    @autoreleasepool {
        Album *album = [[Album alloc] init];
        // Dot syntax is sugar for the accessor messages.
        album.title = @"Head Hunters";
        album.year = 1973;
        NSLog(@"%@ (%ld)", album.title, (long)album.year);
    }
    return 0;
}

How it works

  1. @property (copy) NSString *title synthesizes everything.
  2. Dot syntax is sugar for the accessor messages.
  3. (copy) snapshots the string instead of sharing it.

Keywords and builtins used here

The run, in numbers

Lines
20
Characters to type
453
Tokens
83
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 77 seconds.

Type this snippet

Step 2 of 3 in Classes, step 8 of 27 in Language basics.

← Previous Next →

Properties in other languages