将 nil 传递给 PerformSelector

发布于 2024-11-06 10:50:15 字数 469 浏览 0 评论 0原文

我正在使用 NSObject 的 PerformSelector 方法来调用基于运行时确定的变量字符串的方法。

[self performSelector:NSSelectorFromString(methodName)];

方法调用在循环内。但是,在某些循环迭代中,我不想执行任何操作,并且 methodName 可能为零。 看来performSelector消息不接受nil作为参数。当我不想让 PerformSelector 执行任何操作时我该怎么办?

编辑: 我应该说得更清楚。现在,我正在设置 methodName = @"doNothing" - 顾名思义 - 当我希望它为零时。我想知道是否有比这更优雅的解决方案,或者只是放置一个 if 块。 我想我的问题是 - 是否有空类型选择器?

I am using the NSObject's performSelector method to call a method based on a variable string determined at runtime.

[self performSelector:NSSelectorFromString(methodName)];

The method call is within a loop. However, on some loop iterations I don't want to do anything and methodName may be nil.
It seems that the performSelector message does not take nil as an argument. What can I do when i don't want performSelector to perform any actions?

Edit:
I should have been more clear. For now, i am setting methodName = @"doNothing" - which is what is name implies - when I want it to be nil. I want to know if there is a more elegant solution than this, or just putting an if block.
I guess my question is - Is there a null type selector?

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

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

发布评论

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

评论(3

爱人如己 2024-11-13 10:50:16

检查是否为nil

SEL selector = NSSelectorFromString(methodName);
if (selector != nil) {
  [self performSelector:selector];
}

Check to see if it's nil?

SEL selector = NSSelectorFromString(methodName);
if (selector != nil) {
  [self performSelector:selector];
}
国粹 2024-11-13 10:50:16

您可能正在寻找这个 -

if ([self respondsToSelector:NSSelectorFromString(methodName)]) {
    [self performSelector:NSSelectorFromString(methodName)];
}

You may be looking for this -

if ([self respondsToSelector:NSSelectorFromString(methodName)]) {
    [self performSelector:NSSelectorFromString(methodName)];
}
南巷近海 2024-11-13 10:50:16
if (methodName != nil) {
    [self performSelector:NSSelectorFromString(methodName)];
}
if (methodName != nil) {
    [self performSelector:NSSelectorFromString(methodName)];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文