问题
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?
回答1:
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.
回答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];
}
回答3:
This sort of thing is best achieved with an array.
Similar question here: Objective C Equivalent of PHP's "Variable Variables"
回答4:
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.
来源:https://stackoverflow.com/questions/11073433/incrementing-variable-names-in-xcode