【问题标题】:Objective-c How to send a (void) in parameter?Objective-c如何在参数中发送(void)?
【发布时间】:2011-07-20 14:40:29
【问题描述】:
我想知道如何在参数中传递 void 的“地址”?
- (void) showWithLabel:(id)method {
...
[HUD showWhileExecuting:@selector(method) onTarget:self withObject:nil animated:YES];
}
- (void) test:{
sleep(3)
}
- (void) hello:{
[self showWithLabel:(id)test]
}
但这不起作用(构建错误)
【问题讨论】:
标签:
iphone
objective-c
ipad
call
【解决方案1】:
要传递选择器,您需要以下关键字:
@selector 和 SEL。
所以,在你的例子中,它应该是:
- (void) showWithLabel:(SEL)method {
和
[self showWithLabel:@selector(test)]
调用选择器:
[目标执行选择器:方法]
正如 Kubi 指出的,你应该非常小心冒号。
如果没有参数,则没有冒号。
但是,如果你有参数,你必须意识到冒号是选择器名称的一部分。
例如:
-(void) test //no parameter gives @selector(test)
-(void) testWithName:(NSString*)name //1 parameter gives @selector(testWithName:)
-(void) testWithName:(NSString*)name andAge:(int)age //2paramater gives selector gives @selector(testWithName:andAge:)
【解决方案2】:
没有“虚无”这样的东西。 void 是一个 C 关键字,当应用于指针时,意味着指针是无类型的,即类型未知或不重要。我最近注意到一些初学者似乎将 void 方法或函数(即不返回值的方法或函数)称为“void”,但这并不常见。
我无法从您发布的代码中看出您想传递什么或传递到哪里。您能否修改您的问题以使其更清楚?
【解决方案3】:
还请注意,我删除了您的 test 和 hello 方法上的冒号。在 Obj-C 中,冒号表示方法参数,因为没有参数,所以不需要冒号。
- (void) showWithLabel:(SEL)method {
//...
[HUD showWhileExecuting:method onTarget:self withObject:nil animated:YES];
}
- (void) test {
sleep(3)
}
- (void) hello {
[self showWithLabel:@selector(test)]
}