如何将数组传递给期望使用var args的objc方法(例如...') [英] How to pass an array to an objc method that expects var args (eg ...')

查看:138
本文介绍了如何将数组传递给期望使用var args的objc方法(例如...')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在库中有一个看起来像这样的方法:

I have a method in a library that looks like so:

- (id)initWithSomeObjects:(NSString *)something, ... NS_REQUIRES_NIL_TERMINATION;

我真的很想用数组而不是var args来调用它,因为我想传递的对象数量是可变的.

I'd really like to call it with an array instead of var args, because the number of objects i'd like to pass in is changeable.

是否有某种方法,可以使用performSelector或NSInvocation或objc_msgSend或其他任何方法来调用var args方法,并且参数来自数组?

Is there some way, using performSelector or NSInvocation or objc_msgSend or whatever, that i can call the var args method, with the arguments coming from an array?

推荐答案

没有简单的方法,因为如何传递参数进入特定系统调用ABI的丑陋细节,您必须了解例如将多少个参数放入寄存器中,以及如何处理其余的参数等.这将涉及汇编,并且无法以一般方式完成.

There is no easy way to do this, because how arguments are passed goes into the ugly details of the particular system's calling ABI, and you have to know e.g. how many arguments are put into registers, and how to deal with remaining arguments, etc. And this will involve assembly and it cannot be done in a general way.

通常,任何具有这样的方法或函数的api都将采用这样的varargs,

Generally, any API that has a method or function that takes varargs like this, also will either

  1. 提供另一种采用va_list参数的方法(例如-[NSString initWithFormat:]具有-[NSString initWithFormat:arguments:]).如果是这种情况,那么您可以使用该技术从评论中链接到的文章中构造va_list. (即使构造va_list都是特定于系统的,并且不可移植.但是至少它可以在Mac和iPhone上使用,并且这样做相当简单,并且不涉及组装.)
  2. 提供另一种采用NSArray *或C元素数组的方法(例如-[NSArray initWithObjects:]具有-[NSArray initWithObjects:count:])
  3. 有一种方法可以逐个添加元素,以使整体效果与将所有元素一起传递给varargs方法相同(例如,可以通过调用-[UIAlertView addButtonWithTitle:] multiple来实现-[UIAlertView initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:]末尾的varargs按钮标题)次).
  1. Provide another method that takes a va_list parameter instead (e.g. -[NSString initWithFormat:] has -[NSString initWithFormat:arguments:]). If this is the case, then you can use the technique to construct the va_list from the article you linked to in the comments. (Even constructing the va_list is system-specific and non-portable. But at least it works on Mac and iPhone and it's fairly simple to do and does not involve assembly.)
  2. Provide another method that takes an NSArray * or a C array of elements (e.g. -[NSArray initWithObjects:] has -[NSArray initWithObjects:count:])
  3. Have methods to add elements one by one such that the overall effect is the same as passing them all together to the varargs method (e.g. the varargs button titles at the end of -[UIAlertView initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:] can be achieved by calling -[UIAlertView addButtonWithTitle:] multiple times).

如果您使用的varargs API没有以上任何一项,则说明API设计不良,您应该向编写该代码的人投诉.如果您真的遇到这种情况,我想可能会使用libffi之类的东西,它允许您动态地进行函数调用,并处理与系统相关的调用机制的细节.

If you have a varargs API that doesn't have one of the above, then it's a badly designed API and you should complain to whoever wrote it. If you really run into such a situation, I am guessing that it would be possible to use something like libffi, which allows you to make function calls dynamically, and handles the nitty gritty of system-dependent calling mechanisms.

这篇关于如何将数组传递给期望使用var args的objc方法(例如...')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆