0 cocoa-touch objective-c variadic-functions xcode4.2
我需要在我的方法中接收多个变量Args.但我不知道该怎么做.
例如:
(void)insertInTableOnAttributes:(id)fieldsNames, ... Values:(id)fieldsValues, ...;
Run Code Online (Sandbox Code Playgroud)
遗憾的是,在第(...)一句话后出现编译错误:
Expected ':' after method Prototype".
Run Code Online (Sandbox Code Playgroud)
在实施中说:
Expected Method Body" in the same position (just after the first ...)
Run Code Online (Sandbox Code Playgroud)
PD:我正在使用Xcode 4.2.1.
你不能这样做.生成的代码如何知道一个参数列表的结束位置和下一个参数列表的开始位置?试着想一下C等价物
void insertInTableOnAtributes(id fieldNames, ..., id fieldValues, ...);
Run Code Online (Sandbox Code Playgroud)
出于同样的原因,编译器将拒绝它.
你有两个合理的选择.第一个是提供一个NSArray代替s 的方法.
- (void)insertInTableOnAttributes:(NSArray *)fieldNames values:(NSArray *)fieldValues;
Run Code Online (Sandbox Code Playgroud)
第二种是使用一个具有名称 - 值对的varargs,类似于 +[NSDictionary dictionaryWithObjectsAndKeys:]
- (void)insertInTableOnAttributes:(id)fieldName, ...;
Run Code Online (Sandbox Code Playgroud)
这个就像用的那样
[obj insertInTableOnAttributes:@"firstName", @"firstValue", @"secondName", @"secondValue", nil];
Run Code Online (Sandbox Code Playgroud)
C类比实际上非常准确.Obj-C方法基本上是基于C方法的语法糖,所以
- (void)foo:(int)x bar:(NSString *)y;
Run Code Online (Sandbox Code Playgroud)
由看起来像的C方法支持
void foobar(id self, SEL _cmd, int x, NSString *y);
Run Code Online (Sandbox Code Playgroud)
除了它实际上没有真名.此C函数称为IMP方法,您可以使用obj-c运行时方法检索它.
如果你在varargs之后有争论,你的
- (void)someMethodWithArgs:(id)anArg, ... andMore:(id)somethingElse;
Run Code Online (Sandbox Code Playgroud)
会得到一个IMP看起来像的支持
void someMethodWithArgsAndMore(id anArg, ..., id somethingElse);
Run Code Online (Sandbox Code Playgroud)
并且由于你在varargs之后不能有任何参数,这根本不起作用.
| 归档时间: |
|
| 查看次数: |
449 次 |
| 最近记录: |