我正在使用 AWS,需要 AWSTask 方面的帮助。基本上我有一些非常大的 block ,我想将它们声明为变量以提高可读性。
http://docs.aws.amazon.com/mobile/sdkforios/developerguide/awstask.html
例如这段代码:
[task continueWithSuccessBlock:^id(AWSTask *task) {
//do something
return nil;
}];
如何将 block 提取为局部变量?这是我到目前为止所得到的:
void (^myBlock)(AWSTask *task) = ^(AWSTask *task){
//做点什么
返回零;
};
[任务 continueWithSuccessBlock:myBlock];
这给出了一个错误:
Incompatible block pointer types initializing 'void (^__strong)(AWSTask *__strong)' with an expression of type 'void *(^)(AWSTask *__strong)'
方法声明如下:
- (AWSTask *)continueWithSuccessBlock:(AWSContinuationBlock)block;
这里是涉及的typedef:
typedef __nullable id(^AWSContinuationBlock)(AWSTask<ResultType> *task);
如有任何指点,我们将不胜感激!
最佳答案
您缺少返回类型 void
。此外,您从 void block 返回 nil
,因此我删除了该行。这是正确的语法:
void (^myBlock)(AWSTask *task) = ^void(AWSTask *task){
//Do something with the AWSTask
};
[task continueWithSuccessBlock:myBlock];
就返回类型而言, block 本质上类似于方法,因为它们必须返回声明的类型。
这是一个有用的引用:http://goshdarnblocksyntax.com/ , 这只是这里更明确命名的域的镜像:http://fuckingblocksyntax.com/
这里有两个来自 Apple 文档的比较不错的引用,尽管它们并不全面:
A Short Practical Guide to Blocks
祝你好运,希望对你有所帮助!
关于amazon-web-services - 使用 AWSTask,如何将 block 声明为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39350614/