incrementing variable names in xcode? [duplicate]

那年仲夏 提交于 2019-12-13 05:49:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!