0

example

add(int a, int b) Here we pass two argument int a, int b,

can we pass n argument with different data types like int,float,long, is it possible?

I means i need to write only one method which handles n Arguments.

kiran kumar
  • 1,349
  • 4
  • 21
  • 40
  • What are you trying to do? This question is way too vague. – bbum Mar 18 '11 at 18:47
  • Which language are you using? And you probably mean generics: http://stackoverflow.com/questions/848641/are-there-strongly-typed-collections-in-objective-c (stackoverflow article about generics in objective c) – Kevin Mar 18 '11 at 18:47
  • For iPhone you program with Objective-c. What you wrote on the top is no objective-c method. But you can pass n arguments. You could use an Array. ;-) I'm sorry, I don't have the time to write a real answer. If there's no answer tomorrow I'll write one. – Sandro Meier Mar 18 '11 at 18:50

3 Answers3

4

Yes, it is possible to pass arguments with different data types to Objective-C methods. I assume you're speaking about Objective-C, since the question is tagged iphone.

For example, you could have a method like:

- (void)foo:(int)foo bar:(float)bar baz:(long)baz {
    // ...
}

If you are talking about C, you could have a function:

void myfunc(int foo, float bar, long baz) {
    // ...
}

If you want to write functions that have variable sized argument lists (so it can take 1 parameter, or 2 parameters, or 3, ...), I suggest you take a look at this blog post which discusses variable argument lists in Objective-C/Cocoa or Apple's technical Q&A on variable argument lists.

ccjensen
  • 4,578
  • 2
  • 23
  • 25
Greg
  • 33,450
  • 15
  • 93
  • 100
0
- (int)add:(int)number1 to:(int)number2 {
         return number1 + number2;
}

call it using [obj add:2 to:4]; where obj is the object receiving the add message.

You may use any type of arguments in the above to achieve what you have in mind. For example:

- (void)add:(int)number1 to:(float)number2 {
         //print result here
}

Standard type conversions etc would apply like in any programming language.

If you are talking about totally arbitrary number of arguments, take a look at Variadic functions in Objective C (Google it). Plenty of good tutorials in the first search results page.

Bourne
  • 10,094
  • 5
  • 24
  • 51
  • Bourne...Thanks for replay. some types my object will messages two arguments or three arguments or ten arguments it will be dynamic. I means its call the add method with two or three or ten arguments. So I need to handle method dynamically. Is it possible.. – kiran kumar Mar 18 '11 at 18:56
  • That's exactly what I've mentioned in the last two lines. Google up on variadic functions in objective C – Bourne Mar 18 '11 at 18:58
  • also see the links in GregInYEG's reply – Bourne Mar 18 '11 at 19:00
0

Anything is possible, but I am pretty certain it would require some serious hacking to get it to work for variable data types. If you can change the requirements to be a variable number of specific data type, then you should look at an excellent tutorial at "Cocoa with Love" : variable-argument-lists-in-cocoa

ccjensen
  • 4,578
  • 2
  • 23
  • 25