语法help – 作为对象名称的variables

可能重复:
根据int数创build多个variables
PHP的“variablesvariables”的目标C等价物

我将如何使用variables创build和引用对象,因为它的名称?

示例 –

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

在Objective-C语言中没有这样的东西,一般来说它不会是一种非常实用的数据引用方式(如果你打错了一个string,编译器将无法捕捉到它)。 我不会进入猜测你真正想做什么(这取决于你的应用程序的这一部分的目标),但是你可以使用一个NSMutableDictionary来获得类似的效果:

 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]; } 

然后再次取出这些值:

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