麻烦简单的数组 - 可可

时间:2012-12-10 16:56:31

标签: objective-c arrays cocoa syntax-error

我想知道是否有可能做到以下几点:

我的方法如下:

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..

}

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

提前致谢!

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

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

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