objective-csyntaxmethod-declaration

Explain the syntax of this particular method declaration


I found this on Github, but I do not know what this code is doing. Can anyone please explain?

- (id)initWithTitle:(NSString *)title 
            message:(NSString *)message 
    completionBlock:(void (^)(NSUInteger buttonIndex))block 
  cancelButtonTitle:(NSString *)cancelButtonTitle 
  otherButtonTitles:(NSString *)otherButtonTitles, ... {

In particular what is this (void (^) thing, and how is the ... used at the end?


Solution

  • The (void (^)(NSUInteger buttonIndex))block, as the label and parameter name indicate, is a Block, which is a chunk of runnable code that is also a first-class object.

    The ellipsis, ..., indicates that the method takes a variable number of final arguments. This functionality is commonly known by its C library name, "varargs". The more formal term is "variadic".