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

    Joined:
    Feb 22, 2008
    #1
    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...

    Code:
    this["sq"+i]
    ...or something like that. Is there an Objective-C equivalent?
     
  2. macrumors member

    Joined:
    Jul 27, 2008
    #2
    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. thread starter macrumors newbie

    Joined:
    Feb 22, 2008
    #3
    Cool, thanks!
     
  4. macrumors 65816

    Dippo

    Joined:
    Sep 27, 2003
    Location:
    Charlotte, NC
    #4
    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