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

问题描述 投票:0回答:2

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

- (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)'

你们都知道我在做什么错,或者我该如何解决?

非常感谢您的帮助。

ios objective-c objective-c-blocks xcode11.4
2个回答
0
投票
指针。

因此,请添加*,如下所示

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;

0
投票
- (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]; }
© www.soinside.com 2019 - 2024. All rights reserved.