typestar

NSString in Objective-C

@"" literals are objects; messages do all the work.

#import <Foundation/Foundation.h>

int main(void) {
    @autoreleasepool {
        // @"" literals are NSString objects; messages do the work.
        NSString *artist = @"Alice Coltrane";
        NSString *label = [NSString stringWithFormat:@"%@ -- %d press",
                           artist, 1971];

        NSLog(@"%@", label);
        NSLog(@"Length: %lu", (unsigned long)[label length]);
        NSLog(@"Shout: %@", [artist uppercaseString]);
        NSLog(@"Match? %d", [artist isEqualToString:@"Alice Coltrane"]);
    }
    return 0;
}

How it works

  1. stringWithFormat: is the printf of object creation.
  2. length and uppercaseString are messages, not properties.
  3. isEqualToString: compares contents; == would compare pointers.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
449
Tokens
96
Three-star pace
65 tpm

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

Type this snippet

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

← Previous Next →