在@selector()中传递一个块 [英] passing a block in @selector()
问题描述
如何在方法incrementCount:completion
中传递块,以及它的外观如何,以使属性self.count
在CounterClass中递增后返回?我不确定在方法中定义块参数(void(^)(void))callback;
的方式是否正确,即是否也应该有返回值?
How do I pass a block, and what would it look like, in the method incrementCount:completion
to get the property self.count
returned after its increment in the CounterClass? I'm not sure if the way I defined the block parameter (void(^)(void))callback;
in the method is correct i.e. should it also have a return value?
ViewController
ViewController
[NSTimer scheduledTimerWithTimeInterval:3.0
target:self.counterClass
selector:@selector(incrementCount:completion:)
userInfo:nil
repeats:YES];
CounterClass
CounterClass
-(void)incrementCount:(NSTimer *)timer completion:(void(^)(void))callback;
{
self.count += 1;
}
推荐答案
NSTimer
期望调用一个带有零个或一个参数的方法,如果有参数,则它应该是计时器实例本身.
NSTimer
expects to call a method which takes zero or one parameters, if there is a parameter it should be the timer instance itself.
因此,您无法使用带有2个参数的方法,其中一个是一个块.
So, you can't have a method with 2 parameters where one is a block.
相反,删除第二个参数,然后仅调用方法实现中的另一个方法或块.该块可以存储为该类的@property.
Instead, remove the second parameter and simply call another method or block in the method implementation. The block could be stored as an @property of the class.
这篇关于在@selector()中传递一个块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!