2012-12-10 49 views
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.. 

} 

如何使用字符串标识数组并操纵它?

在此先感谢!

+0

我真的怀疑你想这样做的原因。 – Almo

+0

因为我的应用程序,我简化了课程当然.. 我正在使用表和SQL数据,但因为这是我第一次有很多麻烦... 基本上我有一个表与许多SQL表中的字段,并需要相对阵列选择点击的表.. 相当困难的都为我和我的英文不好.. 感谢解释反正 –

+0

你的英语没有那么糟糕。 :)感谢您的背景。 – Almo

回答

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

非常感谢,这真的帮了我很大的忙! –

4

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

+0

非常感谢! –