使用类型表达式初始化“void(^)(struct ALAssetsGroup *, BOOL *)”时不兼容的块指针类型

发布于 2024-11-27 16:58:22 字数 1144 浏览 2 评论 0原文

我看到 SO 上有几个人已经成功地使用了这段代码。但我收到了不兼容的块指针错误:

不兼容的块指针类型

void(^)(struct ALAssetsGroup *, BOOL *)

用类型的表达式

void(^)(ALAsset *, NSUInteger, BOOL *)

初始化有任何提示吗? (使用完整代码进行编辑)

    ALAssetsLibrary *library =[[ALAssetsLibrary alloc]init];
    void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop){
    if(result != NULL) {
                NSLog(@"See Asset: %@", result);

            }
        };

    void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) {
            if(group != nil) {NSLog(@"dont See Asset: ");
                [group enumerateAssetsUsingBlock:assetEnumerator];
            }
        };

    [library enumerateGroupsWithTypes:ALAssetsGroupAlbum
                               usingBlock:assetGroupEnumerator
                             failureBlock: ^(NSError *error) {
                                 NSLog(@"Failure");
                             }];

在此处输入图像描述

I saw several people on SO have been using this code successfully. But I got the incompatible block pointer error:

Incompatible block pointer types initializing

void(^)(struct ALAssetsGroup *, BOOL *)

with an expression of type

void(^)(ALAsset *, NSUInteger, BOOL *)

Any hints? (EDIT with complete code)

    ALAssetsLibrary *library =[[ALAssetsLibrary alloc]init];
    void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop){
    if(result != NULL) {
                NSLog(@"See Asset: %@", result);

            }
        };

    void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) {
            if(group != nil) {NSLog(@"dont See Asset: ");
                [group enumerateAssetsUsingBlock:assetEnumerator];
            }
        };

    [library enumerateGroupsWithTypes:ALAssetsGroupAlbum
                               usingBlock:assetGroupEnumerator
                             failureBlock: ^(NSError *error) {
                                 NSLog(@"Failure");
                             }];

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淑女气质 2024-12-04 16:58:22

好的,区块新手...但我在这里找到了资产组枚举器块的另一个示例,并且它的声明中没有 struct 。我尝试从上面的代码中删除它,它仍然工作正常并且没有错误消息。希望更了解struct的人可以解释一下吗?

尝试将这一行更改

void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) 
            = ^(ALAssetsGroup *group, BOOL *stop)

为:

void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) 
            = ^(ALAssetsGroup *group, BOOL *stop)

我认为底线是 ALAssetsLibrary enumerateGroupsWithTypes: usingBlock: 期望一个看起来像 (ALAssetsGroup *, BOOL *) 的块,而不是 (struct ALAssetsGroup *, BOOL *)

OK, newbie at blocks... but I found another example of an asset group enumerator block on here, and it didn't have struct in the declaration. I tried removing it from the code above, and it still works fine and doesn't have the error message. Hopefully someone who understands struct better can explain?

try changing this line:

void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) 
            = ^(ALAssetsGroup *group, BOOL *stop)

to this:

void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) 
            = ^(ALAssetsGroup *group, BOOL *stop)

I think the bottom line is that the ALAssetsLibrary enumerateGroupsWithTypes: usingBlock: expects a block looking like (ALAssetsGroup *, BOOL *) not (struct ALAssetsGroup *, BOOL *).

岛徒 2024-12-04 16:58:22

预期类型和实际类型之间的区别仅在于工作struct,即struct ALAsset*ALAsset*。 (在您的文字描述中,ALAssetALAssetGroups 之间看起来不匹配,但我认为您在复制错误消息时犯了一个错误。)

我不太明白在哪里这些差异来自(可能是由于在某处使用了 C++?)。

无论如何,最好的解决方案是分别使用类型定义 ALAssetsGroupEnumerationResultsBlockALAssetsLibraryGroupsEnumerationResultsBlock,例如:

ALAssetsGroupEnumerationResultsBlock assetEnumerator = ^(ALAsset *result, NSUInteger index, BOOL *stop){
    if (result != NULL) {
            NSLog(@"See Asset: %@", result);
        }
    };

The difference between the expected and the actual type is just the work struct, i.e. struct ALAsset* vs. ALAsset*. (In your textual description it looks like a mismatch between ALAsset and ALAssetGroups, but I think you made a mistake in copying the error message.)

I don't quite understand where these differences come from (possibly due to the use of C++ somewhere?).

Anyway, the best solution is to use the type definition ALAssetsGroupEnumerationResultsBlock or ALAssetsLibraryGroupsEnumerationResultsBlock respectively, e.g.:

ALAssetsGroupEnumerationResultsBlock assetEnumerator = ^(ALAsset *result, NSUInteger index, BOOL *stop){
    if (result != NULL) {
            NSLog(@"See Asset: %@", result);
        }
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文