Objective-c 如何在参数中发送(void)?

发布于 2024-10-24 21:23:09 字数 286 浏览 2 评论 0原文

我想知道如何传入参数 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]
}

但这不起作用(构建错误)

i would like to know how can i pass in parameter the "address" of a 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]
}

But that doesnt work (build error)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月亮邮递员 2024-10-31 21:23:10

另请注意,我删除了 testhello 方法中的冒号。在 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)]
}

Also notice that I removed the colons on your test and hello methods. In Obj-C a colon delineates method parameters and since you had no parameters, you need no colons.

- (void) showWithLabel:(SEL)method {
    //...
    [HUD showWhileExecuting:method onTarget:self withObject:nil animated:YES];
}


- (void) test {
    sleep(3)
}

- (void) hello {
    [self showWithLabel:@selector(test)]
}
墨离汐 2024-10-31 21:23:09

要传递选择器,您需要以下关键字:

@selector 和 SEL。

因此,在您的示例中,它应该是:

- (void) showWithLabel:(SEL)method {

[self showWithLabel:@selector(test)]

调用选择器:
[target PerformSelector:method]

正如 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:)

To pass a selector, you need the following keywords :

@selector and SEL.

So, in your example, it should be:

- (void) showWithLabel:(SEL)method {

and

[self showWithLabel:@selector(test)]

To call the selector :
[target performSelector:method]

As Kubi points out, you should be very careful with the colon.
There is no colon if there is no parameter.
BUT, if you have parameters, you must realize that the colon is a part of the selector's name.
For example:

-(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:)
云归处 2024-10-31 21:23:09

不存在“空”这样的东西。 void 是一个 C 关键字,当应用于指针时,意味着该指针是无类型的,即类型未知或不重要。我最近注意到一些初学者似乎将 void 方法或函数(即不返回值的方法或函数)称为“void”,但这不是常见用法。

我无法从您发布的代码中看出您想要传递什么或想要传递到哪里。您能否修改您的问题以使其更清楚?

There's no such thing as "a void". void is a C keyword that, when applied to a pointer, means that the pointer is untyped, i.e. the type is unknown or not important. I've noticed recently that some beginners seem to refer to a void method or function (that is, a method or function that doesn't return a value) as "a void", but this is not common usage.

I can't tell from the code you posted what you want to pass or where you'd like to pass it. Could you revise your question to make it more clear?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文