typestar

NSArray in Objective-C

@[] builds an array; subscripts and messages read it.

#import <Foundation/Foundation.h>

int main(void) {
    @autoreleasepool {
        // @[] builds an NSArray; subscripts read it.
        NSArray<NSString *> *bins = @[ @"jazz", @"soul", @"dub" ];
        NSLog(@"%lu bins, first is %@",
              (unsigned long)[bins count], bins[0]);

        NSLog(@"Last: %@", [bins lastObject]);
        NSLog(@"Has funk? %d", [bins containsObject:@"funk"]);
        NSLog(@"All: %@", [bins componentsJoinedByString:@" / "]);
    }
    return 0;
}

How it works

  1. @[ @"jazz", @"soul" ] is the literal form.
  2. bins[0] subscripts; count and lastObject are messages.
  3. componentsJoinedByString: renders the whole array.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
414
Tokens
105
Three-star pace
65 tpm

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

Type this snippet

Step 1 of 3 in Collections, step 10 of 27 in Language basics.

← Previous Next →