Syntax help - a variable as an object name

Possible duplicate:
create multiple variables based on int number
Goal C Equivalent to PHP variable variables

How do I create and reference an object using a variable as a name?

Example -

for (int i=1; i<7; i++) { CGRect ("myRectNum & i") = myImageView.bounds; } ("myRectNum & 5").height etc .. 
+4
source share
1 answer

Objective-C has nothing of the kind, and overall this is not a very practical way to access data (what if you seal a string? The compiler cannot catch It). I will not intrude on what you really want to do (it will depend on the purpose of this part of your application), but you can use NSMutableDictionary to get a similar effect:

 NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; for (int i = 0; i < 7; i++) { NSString *key = [NSString stringWithFormat:@"myRectNum & %d", i]; NSValue *value = [NSValue valueWithCGRect:myImageView.bounds]; [dict setObject:value forKey:key]; } 

Then to return the values ​​back:

 NSValue *value = [dict objectForKey:@"myRectNum & 5"]; CGRect bounds = [value CGRectValue]; NSLog(@"height = %f", bounds.size.height); 
+2
source

All Articles