1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

How do i declare array values with variable?

Discussion in 'iPhone/iPad Programming' started by teamstar, Mar 25, 2012.

  1. macrumors newbie

    Hi all,

    For Objective C, the basic of declaring arrays is as such:
    NSArray *numbers;
    NSString *string;
    numbers = [NSArray arrayWithObjects: @"One", @"Two", @"Three", 
    How do i insert a predeclared variable as the values? e.g.:
    NSString *var1 = @"This is var1";
    NSInteger *var2 = 25;
    NSInteger *var3 = 43;
    NSArray *array1 = [NSArray arrayWithObjects: var1, var2, var3, nil];
    I do apologize if i have presented a foolish issue.
    Thanx in advance.
  2. macrumors 68000

    NSArray and other container objects only take objects, so you have to convert those numbers into an object that contains it. Look into the NSNumber class.

    NSString *var1 = @"This is var1";
    NSNumber *var2 = [NSNumber numberWithInteger: 25];
    NSNumber *var3 = [NSNumber numberWithInteger: 43];
    NSArray *array1 = [NSArray arrayWithObjects: var1, var2, var3, nil];
  3. macrumors 603


    Actually, in the interest of using less memory, why not just use C-Arrays? They're not as powerful as NSArrays, but they may just do everything you need.

    Declaration looks kind of like this:

    <type> <name>[#] = {%, %, %...};
    Where <type> is a c var type, like int or float or char, that you'll be storing in the array. <name> is the name of your array, # is the number of variables you'll have in your array, and each of the %s separated by commas would be the initial values.

    Accessing something from the array would look like:

    Where <name> is the name of your array and # is the index of the item in your array (keep in mind they're zero indexed, so the first var is at index 0, not 1).

    To set something in the array you could use:

    <name>[#] = %;
    Where % is the value you're assigning the variable at index # of c variable <name>.

    # for the setter/getter is just an int, which means it can be used in a for loop, for example, where on each iteration it sets a different index of the array to a different value.

    Frequently, if I want the array to have a dynamic number of variables in it, I'll just set it to be the maximum size it could possibly be and have a seperate int variable keep track of how many variables are actually stored in it.
  4. macrumors 68000

    The OPs requirement includes strings too. Perhaps an array of pointers would be better?

    Mutable container classes solve your issue of dynamic number of variables with little work on your part. Also Apple has put a lot of work into keeping the performance high. Their weakness is not being able to handle non-object types without conversion.

    If you want to stay in the strict C realm and have very large or an unknown quantity of vars to track, consider looking into linked lists.
  5. macrumors 603


    I don't know how much work Apple has put into it, but using C arrays full of ints is many times quicker than NSArrays full of NSNumbers.

    There is a series of articles comparing the performance of the two,

    Part 1:
    (Finds that C Arrays and floats are 100-500+ times faster than NSArrays and NSNumbers)

    Part 2:
    (Finds that if you're making objects perform selectors, C Arrays are still 5.8-8 times faster.)

    Part 3:
    (Slightly modifies the code from part 2 and is able to get NSArrays to be half as fast as C Arrays on their iPhone 3G and the exact same speed on their iPhone 3GS.)
  6. macrumors 68000

    Thats a good thing to point out. Even given container optimizations, there may still be good reasons not to use them. We don't know the whole story for the OP.
  7. macrumors newbie

    This is something i am very interested in. I cannot however understand heads or tails about the coding but i understand what the links summarize.
  8. macrumors 603


    Note that you don't really need C-array performance most of the time. I use them in my games with physics, when I need to check whether any of 50+ objects have collided with each other 50-100 times a second.

    If you're only dealing with 10 objects or so 10 times a second, NSArray should be plenty fast (and possibly easier to use.)

Share This Page