Objective-C不兼容的块类型错误

时间:2015-10-26 23:52:57

标签: objective-c

我有这个方法:

- (void)shareItems:(NSArray *)shareItems fromViewController:(UIViewController *)viewController anchorView:(UIView *)anchorView completion:(void (^)(NSString *activityType, BOOL, NSError *))completionHandler;

enter image description here

我只是想在XCode 7.0.1中实现它,我收到以下错误消息:

如果你看不到,我试图实施:

[[VMSocialShareManager defaultManager] shareItems:shareItems fromViewController:self anchorView:shareCell completion:^(NSString *activityType, BOOL completed,  NSError *error) {
    ...
}];

但是得到错误:

Incompatible block pointer types sending 'void (^)(NSString *__strong, BOOL, NSError *__strong)' to parameter of type 'void (^)(BOOL, NSString *__strong, NSError *__strong)'

好的,当然,我会尝试翻转BOOLNSString,即使这没有任何意义:

[[VMSocialShareManager defaultManager] shareItems:shareItems fromViewController:self anchorView:shareCell completion:^(BOOL completed, NSString *activityType, NSError *error) {
    ...
}];

但是那会产生错误:

Incompatible block pointer types sending 'void (^)(BOOL, NSString *__strong, NSError *__strong)' to parameter of type 'void (^)(NSString *__strong, BOOL, NSError *__strong)'

2 个答案:

答案 0 :(得分:0)

您的声明中您的块中没有其他两个项目的名称。它应该是:

- (void)shareItems:(NSArray *)shareItems 
fromViewController:(UIViewController *)viewController 
        anchorView:(UIView *)anchorView 
        completion:(void (^)(NSString *name, BOOL flag, NSError *error))completionHandler;

您缺少flagerror参数名称。

编辑:为了显示参数的顺序无关紧要,我在AppDelegate中使用上述方法签名创建了一个测试项目,并在application:didFinishLaunchingWithOptions:中调用它。它按预期运行。我已附上示例项目here

答案 1 :(得分:0)

所以,事实证明我只需将activityType置于中间,successerror之间。不知道为什么,但它现在编译:

- (void)shareItems:(NSArray *)shareItems fromViewController:(UIViewController *)viewController anchorView:(UIView *)anchorView completion:(void (^)(BOOL success, NSString *activityType, NSError *error))completionHandler;