如何将参数列表应用于 Objective C 中的块?
Objective C 是否有类似 Smalltalk 的 Block valueWithArguments
的功能?
我正在寻找一个具有如下签名的函数:
apply(^(), NSArray* args)
或者,有没有办法通过参数列表调用选择器?
Does Objective C have a function like Smalltalk's Block valueWithArguments
?
I'm looking for a function with a signature like:
apply(^(), NSArray* args)
Alternatively, is there a way to call a selector over a list of arguments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于块,您可能需要执行一些
va_args
魔法。可以使用可变参数调用选择器,但您可能需要一些帮助器来减轻痛苦。NSObject+performSelectorWithArgsArray.h
NSObject+performSelectorWithArgsArray.m
然后像这样使用它:
如果你选择做这样的事情,我建议你真正阅读一下
NSInitation
的工作原理,因为它会非常困难。不确定是否可以在不要求所有参数和返回值都是对象的情况下执行此操作。With blocks you probably have to do some
va_args
magic. Calling a selector with variable arguments can be done but you probably want some helpers to make it a bit less painful.NSObject+performSelectorWithArgsArray.h
NSObject+performSelectorWithArgsArray.m
And then use it like this:
If you choose to do something like this I recommend that you really read up on how
NSInvocation
works as it can bite quite hard. Not sure if it is possible to do this without requiring that all arguments and return value are objects.NSArray
具有方法enumerateObjectsUsingBlock:
和enumerateObjectsWithOptions:usingBlock:
是您正在寻找的吗?NSArray
has the methodsenumerateObjectsUsingBlock:
andenumerateObjectsWithOptions:usingBlock:
is that what you are looking for?