typestar

Selectors in Objective-C

Method names interleave with their arguments, part by part.

#import <Foundation/Foundation.h>

int main(void) {
    @autoreleasepool {
        // A selector's name interleaves with its arguments: this one
        // is stringByReplacingOccurrencesOfString:withString:.
        NSString *sleeve = @"mono pressing, mono mix";
        NSString *fixed =
            [sleeve stringByReplacingOccurrencesOfString:@"mono"
                                              withString:@"stereo"];
        NSLog(@"%@", fixed);

        // Ranges pair a location with a length.
        NSRange hit = [fixed rangeOfString:@"stereo"];
        NSLog(@"First hit at %lu, length %lu",
              (unsigned long)hit.location, (unsigned long)hit.length);
    }
    return 0;
}

How it works

  1. stringByReplacingOccurrencesOfString:withString: is one name.
  2. Each colon marks where an argument slots in.
  3. rangeOfString: returns an NSRange struct, read with dots.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
549
Tokens
90
Three-star pace
60 tpm

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

Type this snippet

Step 2 of 3 in Messages, step 5 of 27 in Language basics.

← Previous Next →