Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在ObjC方法中传递多个变量Args?_Objective C_Cocoa Touch_Xcode4.2_Variadic Functions - Fatal编程技术网

Objective c 如何在ObjC方法中传递多个变量Args?

Objective c 如何在ObjC方法中传递多个变量Args?,objective-c,cocoa-touch,xcode4.2,variadic-functions,Objective C,Cocoa Touch,Xcode4.2,Variadic Functions,我需要在方法中接收多个变量Args。但我不知道怎么做 例如: (void)insertInTableOnAttributes:(id)fieldsNames, ... Values:(id)fieldsValues, ...; 遗憾的是,在第一个(…)之后出现了编译器错误: Expected ':' after method Prototype". 在实施过程中说: Expected Method Body" in the same position (just after the firs

我需要在方法中接收多个变量Args。但我不知道怎么做

例如:

(void)insertInTableOnAttributes:(id)fieldsNames, ... Values:(id)fieldsValues, ...;
遗憾的是,在第一个
(…)
之后出现了编译器错误:

Expected ':' after method Prototype".
在实施过程中说:

Expected Method Body" in the same position (just after the first ...)
PD:我使用的是Xcode 4.2.1

- (void)insertInTableOnAttributes:(NSArray *)fieldsNames values:(NSArray *)fieldsValues;
//使用

[self insertInTableOnAttributes:[NSArray arrayWithObject:@"name", nil] values:[NSArray arrayWithObject:@"value", nil]];
//使用

[self insertInTableOnAttributes:[NSArray arrayWithObject:@"name", nil] values:[NSArray arrayWithObject:@"value", nil]];

你不能那样做。生成的代码如何知道一个参数列表的结束和下一个参数列表的开始?试着想想C的等价物

void insertInTableOnAtributes(id fieldNames, ..., id fieldValues, ...);
编译器将出于同样的原因拒绝该选项

你有两个合理的选择。第一种方法是提供一种采用
NSArray
s的方法

- (void)insertInTableOnAttributes:(NSArray *)fieldNames values:(NSArray *)fieldValues;
第二种方法是使用一个带有名称-值对的varargs,类似于
+[NSDictionary dictionaryWithObjectsAndKeys:][/code>

- (void)insertInTableOnAttributes:(id)fieldName, ...;
这一个可以像这样使用

[obj insertInTableOnAttributes:@"firstName", @"firstValue", @"secondName", @"secondValue", nil];

C的类比实际上相当准确。Obj-C方法基本上是C方法之上的语法糖,所以

- (void)foo:(int)x bar:(NSString *)y;
由一个C方法支持,该方法如下

void foobar(id self, SEL _cmd, int x, NSString *y);
void someMethodWithArgsAndMore(id anArg, ..., id somethingElse);
只是它实际上没有真名。此C函数称为方法的
IMP
,您可以使用obj-C运行时方法检索它

如果在varargs之后有参数,则

- (void)someMethodWithArgs:(id)anArg, ... andMore:(id)somethingElse;
将由一个
IMP
支持

void foobar(id self, SEL _cmd, int x, NSString *y);
void someMethodWithArgsAndMore(id anArg, ..., id somethingElse);

因为在varargs之后不能有任何参数,所以这根本不起作用。

你不能这样做。生成的代码如何知道一个参数列表的结束和下一个参数列表的开始?试着想想C的等价物

void insertInTableOnAtributes(id fieldNames, ..., id fieldValues, ...);
编译器将出于同样的原因拒绝该选项

你有两个合理的选择。第一种方法是提供一种采用
NSArray
s的方法

- (void)insertInTableOnAttributes:(NSArray *)fieldNames values:(NSArray *)fieldValues;
第二种方法是使用一个带有名称-值对的varargs,类似于
+[NSDictionary dictionaryWithObjectsAndKeys:][/code>

- (void)insertInTableOnAttributes:(id)fieldName, ...;
这一个可以像这样使用

[obj insertInTableOnAttributes:@"firstName", @"firstValue", @"secondName", @"secondValue", nil];

C的类比实际上相当准确。Obj-C方法基本上是C方法之上的语法糖,所以

- (void)foo:(int)x bar:(NSString *)y;
由一个C方法支持,该方法如下

void foobar(id self, SEL _cmd, int x, NSString *y);
void someMethodWithArgsAndMore(id anArg, ..., id somethingElse);
只是它实际上没有真名。此C函数称为方法的
IMP
,您可以使用obj-C运行时方法检索它

如果在varargs之后有参数,则

- (void)someMethodWithArgs:(id)anArg, ... andMore:(id)somethingElse;
将由一个
IMP
支持

void foobar(id self, SEL _cmd, int x, NSString *y);
void someMethodWithArgsAndMore(id anArg, ..., id somethingElse);

由于在varargs之后不能有任何参数,这根本不起作用。

C类比是不正确的,因为编译器可能通过命名参数的位置来判断一个列表的结尾和常规参数的开头。@dasblinkenlight:不,实际上是完全正确的。Obj-C方法基本上是C方法之上的语法糖。我会更新我的帖子来解释。好吧,我假设最后一项后面的空格(为零)可以解决这个问题。但是我没有看到“C”的方式。C的类比是不正确的,因为编译器可能通过命名参数的位置来判断一个列表的结束位置和常规参数的开始位置。@dasblinkenlight:不,它实际上完全正确。Obj-C方法基本上是C方法之上的语法糖。我会更新我的帖子来解释。好吧,我假设最后一项后面的空格(为零)可以解决这个问题。但是我没有看到“C”的方式。谢谢,那是B计划,以防我找不到答案。谢谢,那是B计划,以防我找不到答案。