1

我想知道是否有可能执行以下操作:

我有这样的方法:

one = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3", nil];
two  = [[NSMutableArray alloc] initWithObjects:@"4",@"5",@"6", nil];

-(void)getStringAndChooseArray:(NSString *)nameOfArray {
//What i want to do is something like:
NSLog(@"The array %@ has got %i objects",nameOfArray,[nameOfArray count]) 
//Of course it is giving me an error since nameOfArray is a string..

//I know it is hard to understand,
//but what I'm trying to do is to call this method
//pass a string variable, which is named as one of the two arrays,
//and using it to do the rest..

}

如何使用字符串来识别数组并对其进行操作?

提前致谢 !

4

2 回答 2

4

将您的数组存储在字典中,并使用您想要引用它们的名称作为它们的相关键。

于 2012-12-10T16:59:22.837 回答
2

使用字典将数组映射到字符串,然后您可以使用它们:

one = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3", nil];
two  = [[NSMutableArray alloc] initWithObjects:@"4",@"5",@"6", nil];
NSDictionary *mapping = [NSDictionary dictionaryWithObjectsAndKeys:@"one",one,@"two",two,nil];

-(void)getStringAndChooseArray:(NSString *)nameOfArray {
  NSArray *array = [mapping objectForKey:nameOfArray];
  NSLog(@"The array %@ has got %i objects",array,[array count]) 
}
于 2012-12-10T17:00:18.633 回答