What does this syntax mean in Objective-C?

objective-c

Solution

It's an instance method (ie, not a static or "class" method) called `initWithTitle:boxOfficeGross:summary:` that returns an object of type `id` (generic object). It takes three parameters: a String object, a Number object, and another String object.

You invoke it like this:

NSNumber * gross = [NSNumber numberWithInteger:1878025999]
Movie * avatar = [[Movie alloc] initWithTitle:@"Avatar"
                               boxOfficeGross:gross
                                      summary:@"Pocahontas in the 22nd century"];
//or you can do it all on one line, like so:
Movie * avatar = [[Movie alloc] initWithTitle:@"Avatar" boxOfficeGross:gross summary:@"Pocahontas in the 22nd century"];

Problem

Consider the following: ``` - (id)initWithTitle:(NSString *)newTitle boxOfficeGross:(NSNumber *)newBoxOfficeGross summary:(NSString *)newSummary; ``` What does this mean? I've guessed that it returns id, and takes three params, but what does each part of the syntax mean? I come from a Ruby/JS background and am finding this syntax a little hard to grasp.

Original source