I'm just looking to do something along the lines of a dynamic variable name:
Code:
//declared the bgImage above to be a UIImage
// i is either an integer or a nsstring, basically a number
while(i<100){
set("myImage"+i+"View").image = bgImg;
[bgImg release]
//add to i and continue
}
hasn't anyone done this in their program rather than write out 100 different myImage1View, myImage2View, myImage3View etc? if so, how? I know it's got to be one easy thing I'm completely overlooking.
Your looking for accessing variables by there name, have you tried just putting the values in an array?
I guess what you want is some sort of Eval() method.
Yeah, while dynamic variable names are fairly common (and useful) in interpreted languages, I don't know how you'd implement them in a compiled language like Obj-C.
I suppose you could do some kind of pointer manipulation-- but that would get rather abstract and difficult to implement, understand, debugg and maintain.
Another suggestion would be to use an NSDictionary of key-value pairs. The Key would be the variable name and the value would be the variable's content.
If the variables were named myVar1...myVarn it would be pretty easy to iterate over the dictionary.
wow. I never knew the languages were so different when it comes to certain characteristics.
I guess the easiest way here is just to go manually with a bunch of if statements in my case, just have 32 different image spots.
Before you spend the time creating 32 sets of duplicate code (and later maintaining them)...
...Have a look at NSArray and NSDictionary. It is easy to create an array where the value of myVar7 is the 7th entry in an array of values (objects).
With NSDictionary there are essentially 2 arrays: one of names and the other of values. So, if your variable names are not incrementally named and/or in no particular order, you can say: From the array of variables, give me the value for the variable named "Bo-Diddley".
I think you will find it a lot easier to code and maintain.