Primitives and formats in Objective-C
C types live alongside objects; BOOL answers YES or NO.
#import <Foundation/Foundation.h>
int main(void) {
@autoreleasepool {
// C primitives live alongside objects; BOOL is YES or NO.
NSInteger stock = 148;
double price = 24.99;
BOOL openSunday = NO;
// Format specifiers: %ld for NSInteger, %.2f, %@ for objects.
NSLog(@"Stock: %ld records", (long)stock);
NSLog(@"Top price: %.2f", price);
NSLog(@"Sunday? %@", openSunday ? @"open" : @"closed");
}
return 0;
}
How it works
NSIntegeranddoubleare plain C under Cocoa names.%ldprints an NSInteger (cast to long);%.2ffixes decimals.- The ternary picks between two
@""literals for %@.
Keywords and builtins used here
BOOLNOdoubleintlongmainreturnvoid
The run, in numbers
- Lines
- 16
- Characters to type
- 407
- Tokens
- 72
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 66 seconds.
Step 2 of 3 in Values, step 2 of 27 in Language basics.