0

我在 Github 上找到了这个,但我不知道这段代码在做什么。谁能解释一下?

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

特别是这个东西是什么(void (^)东西,到底是怎么...用的?

4

2 回答 2

1

void (^) (NSUInteger buttonIndex) 是一个块,它们在整个 Cocoa 框架中使用,我强烈建议在http://developer.apple.com/library/ios/#documentation/上查看 Apple 的入门指南可可/概念/块/文章/bxGettingStarted.html

方法名称末尾的三个点表示该方法采用前一种类型的任意数量的参数,在本例中为 NSString。这与 C 中的 printf 相同。请参阅http://en.wikipedia.org/wiki/Variadic_function#Variadic_functions_in_C.2C_Objective-C.2C_C.2B.2B.2C_and_D

于 2012-07-08T19:47:45.517 回答
1

(void (^)(NSUInteger buttonIndex))block正如标签和参数名称所表明的那样, 是一个Block 它是一块可运行的代码,也是一等对象。

省略号 ,...表示该方法采用可变数量的最终参数。此功能通常以其 C 库名称“varargs”而闻名。更正式的术语是“可变参数”

于 2012-07-08T19:48:06.743 回答