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
@property (copy) NSString *titlesynthesizes everything.- Dot syntax is sugar for the accessor messages.
(copy)snapshots the string instead of sharing it.
Keywords and builtins used here
AlbumNSObjectNSStringcopyintlongreturnvoid
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.
Step 2 of 3 in Classes, step 8 of 27 in Language basics.