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 に答える