PDA

View Full Version : Is there any easy way to reference sequential variables in a for loop?




chaonic
Jul 27, 2008, 11:15 AM
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...

this["sq"+i]

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



Encryptic
Jul 27, 2008, 11:20 AM
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.

chaonic
Jul 27, 2008, 11:36 AM
Cool, thanks!

Dippo
Jul 27, 2008, 11:51 AM
create an array

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.