Objective-C传递块作为参数 [英] Objective-C pass block as parameter
本文介绍了Objective-C传递块作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将Block
传递给Function
/Method
?
我尝试- (void)someFunc:(__Block)someBlock
无济于事.
即. Block
的类型是什么?
推荐答案
块的类型取决于其自变量和返回类型.在一般情况下,块类型的声明方式与函数指针类型的声明方式相同,只是将*
替换为^
.将块传递给方法的一种方法如下:
The type of a block varies depending on its arguments and its return type. In the general case, block types are declared the same way function pointer types are, but replacing the *
with a ^
. One way to pass a block to a method is as follows:
- (void)iterateWidgets:(void (^)(id, int))iteratorBlock;
但是您可以看到,这很混乱.您可以改为使用typedef
来使块类型更整洁:
But as you can see, that's messy. You can instead use a typedef
to make block types cleaner:
typedef void (^ IteratorBlock)(id, int);
然后将该块传递给类似这样的方法:
And then pass that block to a method like so:
- (void)iterateWidgets:(IteratorBlock)iteratorBlock;
这篇关于Objective-C传递块作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文