不兼容的块指针类型初始化的空白(^)(struct ALAssetsGroup *,BOOL *)的表达式的类型

时间:2022-10-09 16:12:01

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");
                             }];

不兼容的块指针类型初始化的空白(^)(struct ALAssetsGroup *,BOOL *)的表达式的类型

2 个解决方案

#1


9  

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?

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

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 *).

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

#2


3  

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.)

期望类型和实际类型之间的区别仅仅是工作结构,即struct ALAsset*和ALAsset*。(在您的文本描述中,它看起来像是ALAsset和ALAssetGroups之间的不匹配,但我认为您复制错误消息是错误的。)

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

我不太明白这些差异从何而来(可能是由于使用了c++)。

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

总之,最好的解决方案是分别使用类型定义ALAssetsGroupEnumerationResultsBlock或ALAssetsLibraryGroupsEnumerationResultsBlock,例如:

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

#1


9  

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?

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

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 *).

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

#2


3  

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.)

期望类型和实际类型之间的区别仅仅是工作结构,即struct ALAsset*和ALAsset*。(在您的文本描述中,它看起来像是ALAsset和ALAssetGroups之间的不匹配,但我认为您复制错误消息是错误的。)

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

我不太明白这些差异从何而来(可能是由于使用了c++)。

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

总之,最好的解决方案是分别使用类型定义ALAssetsGroupEnumerationResultsBlock或ALAssetsLibraryGroupsEnumerationResultsBlock,例如:

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