我正在使用AWS,并且需要有关AWSTask的帮助。基本上,我有一些非常大的块要声明为可读性变量。
http://docs.aws.amazon.com/mobile/sdkforios/developerguide/awstask.html
例如这段代码:
[task continueWithSuccessBlock:^id(AWSTask *task) {
//do something
return nil;
}];
如何将块提取为局部变量?到目前为止,这是我得到的:
void (^myBlock)(AWSTask *task) = ^(AWSTask *task){ //do something return nil; }; [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
。另外,您是nil
从void块返回的,因此我删除了该行。这是正确的语法:
void (^myBlock)(AWSTask *task) = ^void(AWSTask *task){
//Do something with the AWSTask
};
[task continueWithSuccessBlock:myBlock];
在返回类型方面,块本质上类似于方法,因为它们必须返回声明的类型。
这是一个有用的参考:http : //goshdarnblocksyntax.com/,它只是此处更明确命名的域的镜像:http : //fuckingblocksyntax.com/
以下是来自Apple文档的两个不错的参考,尽管它们并不全面:
祝您好运,希望对您有所帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句