-3

I want to make method take infinity arguments in objective-c and add these arguments .

RemoRoid
  • 47
  • 5

2 Answers2

2

I'm assuming by "infinite" you mean "as many as I want, as long, as memory allows".

Pass a single argument of type NSArray. Fill it with as many arguments as you wish.

If all arguments are guaranteed to be non-object datatypes - i. e. int, long, char, float, double, struct's and arrays of them - you might be better of with a NSData. But identifying individual values in a binary blob will be trickier.

Since you want to add them up, I assume they're numbers. Are they all the same datatype? Then pass an array (and old style C array) or a pointer, and also a number of elements.

EDIT: now that I think of it, the whole design is fishy. You want a method that takes an arbitrarily large number of arguments and adds them up. But the typing effort required for passing them into a function is comparable to that of summing them up. If you have a varargs function, Sum(a,b,c,d,e) takes less typing than a+b+c+d+e. If you have a container class (NSArray, NSData, etc), you have to loop through the addends; while you're doing that, you might as well sum them up.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • Can you give me example using NSData by the way you explained ?? – RemoRoid Dec 03 '13 at 19:50
  • Not enough information for that. The NSData approach only makes sense if you have multiple *number* arguments of *different* numeric types. I don't know if that's the case. – Seva Alekseyev Dec 03 '13 at 20:59
2

That's not possible on a finite machine (that is, all existing computers).

If you're good with a variable, yet finite, amount of arguments, there are C's ... variadic argument functions.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200