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

Is there any easy way to reference sequential variables in a for loop?

Discussion in 'iPhone/iPad Programming' started by chaonic, Jul 27, 2008.

  1. macrumors newbie

    Hey guys...

    If I have a bunch of objects named "sq1", "sq2", "sq3", etc. Is there an easy way to reference each from within a for loop?

    For instance, in ActionScript I could do...

    ...or something like that. Is there an Objective-C equivalent?
  2. macrumors member

    Instead of creating hard-coded variables with names which you wish to access sequentially, create an array. Then you just access them with an index.

    Alternative two, you could create a dictionary of the variables and reference them by string, but that doesn't seem like the best option.
  3. macrumors newbie

    Cool, thanks!
  4. macrumors 65816


    I second that!
    An array is a data structure consisting of a group of elements that are accessed by indexing and that is what you need to use in this situation.

Share This Page