Pregunta

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?

¿Fue útil?

Solución

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".

Otros consejos

The void (^) (NSUInteger buttonIndex) is a block, these are used throughout the Cocoa framework and I'd highly recommend looking at Apple's Getting Started Guide at http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxGettingStarted.html

The three dots at the end of the method name indicates that the method takes any number of parameters of the previous type, in this case NSString. This is the same as printf in C. See http://en.wikipedia.org/wiki/Variadic_function#Variadic_functions_in_C.2C_Objective-C.2C_C.2B.2B.2C_and_D

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top