如何获取方法的数量?

发布于 2024-09-16 05:02:11 字数 107 浏览 2 评论 0原文

例如,在 Javascript 中,您可以简单地通过 func.length 获取参数数量(函数应该接受的参数数量)。如何获取 Objective-C 中某个方法的这些信息?

In Javascript, for example, you can get the arity (the number of arguments a function is supposed to take) by simply func.length. How can I get this information for a method in Objective-C?

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

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

发布评论

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

评论(3

眉黛浅 2024-09-23 05:02:11

NSMethodSignature对于所有类型的反射信息都很棒。

SEL selector = @selector(foo:);
NSMethodSignature *sig = [someObj methodSignatureForSelector:selector]
int argCount = [sig numberOfArguments];

NSMethodSignature is awesome for all kind of reflection information.

SEL selector = @selector(foo:);
NSMethodSignature *sig = [someObj methodSignatureForSelector:selector]
int argCount = [sig numberOfArguments];
暮年慕年 2024-09-23 05:02:11

当没有接收者对象可以发送 -methodSignatureForSelector: 时,可以使用 NSStringFromSelector() 并计算冒号字符出现的次数。

When there’s no receiver object to send -methodSignatureForSelector: to, it’s possible to use NSStringFromSelector() and count the number of occurrences of the colon character.

眼泪淡了忧伤 2024-09-23 05:02:11

来自 Objective-C 运行时参考

method_getNumberOfArguments

返回a接受的参数数量
方法。

未签名
method_getNumberOfArguments(方法
method) 参数 method 指向的指针
方法数据结构。通过
有问题的方法。返回值
包含数量的整数
给定接受的参数
方法。

From The Objective-C Runtime Reference

method_getNumberOfArguments

Returns the number of arguments accepted by a
method.

unsigned
method_getNumberOfArguments(Method
method) Parameters method A pointer to
a Method data structure. Pass the
method in question. Return Value An
integer containing the number of
arguments accepted by the given
method.

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