typestar

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

  1. NSInteger and double are plain C under Cocoa names.
  2. %ld prints an NSInteger (cast to long); %.2f fixes decimals.
  3. The ternary picks between two @"" literals for %@.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Values, step 2 of 27 in Language basics.

← Previous Next →