Objective-C block parameter Issue: This block declaration is not a prototype
Asked Answered
C

2

10

I am learning ReactiveObjC , the ReactiveCocoa Objective-C version.

For the code following: In RACSignal.h ,

- (RACSignal *)reduceEach:(id _Nullable (^)())reduceBlock RAC_WARN_UNUSED_RESULT;

(id _Nullable (^)())

Xcode reports a error:

This block declaration is not a prototype

Multiple parameters could be put in the reduceBlock(). As the code following: In UIAlertView+RACSignalSupport.m , and others ,

- (RACSignal *)rac_buttonClickedSignal {
    RACSignal *signal = [[[[self.rac_delegateProxy
        signalForSelector:@selector(alertView:clickedButtonAtIndex:)]
        reduceEach:^(UIAlertView *alertView, NSNumber *buttonIndex){
            return buttonIndex;
        }]
    ......
    return signal;
}

Kinda generic. I think I can put zero or more parameters in the block, with void (^block)() declared.

The syntax is not supported now in Xcode. I want to know how to solve it, and why.

Many Thanks in advance.

Craigcraighead answered 21/12, 2017 at 1:18 Comment(0)
A
20

You can get the "not a prototype" warning when you try to define a function or block prototype using an empty set of parentheses ().

Put a void in the middle of the parens—i.e. (id _Nullable (^)(void)), and you should fix the problem.

Animalism answered 21/12, 2017 at 1:32 Comment(9)
I think , the answer is that way ` (id _Nullable (^)(id _Nullable ,...))`, It's not that easy . (id _Nullable (^)()), I saw the syntax many times.Craigcraighead
@Craigcraighead Did you try it? The default warning settings for Clang in Xcode have gotten stricter about this in recent versions. Putting void in the parens really should fix it.Animalism
I tried. reduceEach:(id _Nullable (^)(void))reduceBlock , And it kills the func reduceEach:^(UIAlertView *alertView, NSNumber *buttonIndex){Craigcraighead
Putting void is recommended by clang. i want to know better solutions. Like NSString stringWithFormat, the parameter is zero or moreCraigcraighead
@Craigcraighead Then just define it as variadic, the way -[NSString stringWithFormat:] does, using an ellipsis. (id _Nullable (^)(NSString *, ...))Animalism
@dengApro: To add to CharlesSrstka's comment, if you use block type with a variadic parameter, then the block implementation needs to actually take a variadic. The type must match the block signature. If you try to call a block expression with a block type that doesn't match the actual signature of the block that it points to at runtime, it will cause undefined behavior.Sherrell
@newacct, I think you can post a answer , and show me some code.Craigcraighead
@Craigcraighead Here's a short example of a variadic function in C.Animalism
Thanks, @Charles Srstka. I did not figure it out today . ReactiveObjC's Objective-C syntax is complicated . i will try tomorrow.Craigcraighead
C
0

If you redefine your signature to accept an array instead of an arbitrary number of parameters, then you’ll workaround it easily

Craigcraighead answered 23/12, 2017 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.