0

Incrementing variable names

In xcode I want to create 100 images one with the name1,2,3,4 ect. And i can do it all except for the variable names. For these I was wandering is it possible to increment the variables programmatically or whether it must be done manually?

Benjamin Gale
  • 12,977
  • 6
  • 62
  • 100
user1305661
  • 11
  • 1
  • 5
  • Why load 100 images into memory if you could simply cache and display... 3 at a time. What exactly might you be doing? – CodaFi Jun 17 '12 at 17:55

4 Answers4

3

Instead of creating a bunch of variable names, you can use a dictionary object with the names as keys and the image object as the data.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
2

If you really want to/have to use 100 different variables and avoid an array you could use Key Value Coding in combination with a for loop:

NSString *name = @"myVariable";
for (int i = 0; i< 100; i++)
{
    // get aValue from somewhere...
    NSString *key = [NSString stringWithFormat:@"%@%d", name, i];
    [self setValue:aValue forKey:key];
}
Stefan Jager
  • 141
  • 1
  • 10
1

This sort of thing is best achieved with an array.

Similar question here: Objective C Equivalent of PHP's "Variable Variables"

Community
  • 1
  • 1
Jezen Thomas
  • 13,619
  • 6
  • 53
  • 91
1

Create them in an NSMutableArray. Doesn't matter if all of them have the same object name, since they have different locations in the array. Just access them with [arrayName objectAtIndex:index]; Use a for.

OFRBG
  • 1,653
  • 14
  • 28