Creating method with indifinitive amount of arguments

Discussion in 'iOS Programming' started by exmorpheus, Mar 25, 2011.

  1. exmorpheus macrumors newbie

    Joined:
    Mar 19, 2011
    #1
    I noticed some array methods like initWithObjects or arrayWithObjects allow you to keep adding arguments until you pass a null argument. So I'm wondering how I would make something similar to that. I looked at the header file in which arrayWithObjects was declared and saw this:

    Code:
    - (void)arrayWithObjects:(id *)firstObject, ... NS_REQUIRES_NIL_TERMINATION;
    
    I was wondering if I could do the same with my own methods and if so, how do I call the other arguments?
     
  2. scottgroovez macrumors regular

    Joined:
    May 20, 2010
    #2
    In other languages you'd parse an array of arguments. You've then just got to work out what to do with it.
     
  3. (marc) macrumors 6502a

    (marc)

    Joined:
    Sep 15, 2010
    Location:
    the woods
    #3
    Use a va_list. It's how functions like printf (and the one you posted, as far as I can tell) work.
     

Share This Page