iosobjective-cobjective-c-blocksxcode11.4

Objective-C Blocks: Incompatible block pointer types


I'm trying to implement a block call. Here is my method:

- (void) runTest; {
    void (^MyBlock)(id, NSUInteger, BOOL) = ^(id obj, NSUInteger idx, BOOL stop) {
        NSLog(@"Video game %@", (NSString *)obj);
    };

    BOOL stop;
    MyBlock(@"Path of exile", 0, &stop);

    NSArray *videoGames = @[@"fallout", @"Deus ex",@"final fintasy"];

    [videoGames enumerateObjectsUsingBlock:MyBlock];
}

But on this line:

[videoGames enumerateObjectsUsingBlock:MyBlock];

I'm getting this error:

Incompatible block pointer types sending 'void (^__strong)(__strong id, NSUInteger, BOOL)' to parameter of type 'void (^ _Nonnull)(id _Nonnull __strong, NSUInteger, BOOL * _Nonnull)'

Any of you knows what I'm doing wrong or how can I fix this?

I'll really appreciate your help.


Solution

  • the Bool parameter of the Block should be a pointer hence you need to add *

    - (void) runTest; {
        void (^MyBlock)(id, NSUInteger, BOOL *) = ^(id obj, NSUInteger idx, BOOL *stop) {
            NSLog(@"Video game %@", (NSString *)obj);
        };
    
        BOOL stop;
        MyBlock(@"Path of exile", 0, &stop);
    
        NSArray *videoGames = @[@"fallout", @"Deus ex",@"final fintasy"];
       [videoGames enumerateObjectsUsingBlock:MyBlock];
    }