如何理解objective-c块的类型?

时间:2014-02-27 05:00:42

标签: objective-c cocoa-touch

- (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock

在此功能签名中,completionBlock的类型为ALAssetsLibraryWriteImageCompletionBlock。但是,因为当我们创建匿名函数时,我们使用

^(int a){
    //code here
}

似乎我们从未指定过任何块的type。那么如何理解这种不寻常的类型呢?

1 个答案:

答案 0 :(得分:1)

如果查看ALAssetsLibraryWriteImageCompletionBlock的文档,则定义为:

typedef void (^ALAssetsLibraryWriteImageCompletionBlock)(NSURL *assetURL, NSError *error);

这是一个没有返回值的块,它有两个参数。

您的代码必须类似于:

[library writeImageDataToSavedPhotosAlbum:someData metadata:someMetaData completionBlock:^(NSURL *assetURL, NSError *error) {
    // completion handler code here with access to assetURL and error
}];