Objective-C 块:不兼容的块指针类型

Objective-C Blocks: Incompatible block pointer types

我正在尝试实施块调用。这是我的方法:

- (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];
}

但是在这条线上:

[videoGames enumerateObjectsUsingBlock:MyBlock];

我收到这个错误:

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

你们中有人知道我做错了什么或者我该如何解决这个问题?

非常感谢你的帮助。

MyBlock的第三个参数应该是BOOL的指针

所以,像下面这样添加 *

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

https://developer.apple.com/documentation/foundation/nsarray/1415846-enumerateobjectsusingblock?language=objc

  • (void)enumerateObjectsUsingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block;

Block 的 Bool 参数应该是一个指针,因此您需要添加 *

- (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];
}