2D阵列Objective-C

时间:2014-04-05 07:15:05

标签: ios objective-c nsmutablearray

我从两个可变数组创建一个数组。 数组1 有3个对象,数组2 我接收到的崩溃表明"无法识别的选择器已发送到实例'"为什么这不起作用?

初始化两个数组:

self.array1 =[[NSMutableArray alloc]initWithObjects:@"word", @"word2", nil];
self.array2 =[[NSMutableArray alloc]initWithObjects:@"this goes with word1", @"this goes with the second word", nil];

NSMutableArray *objects;
for (int i=0 ; i<[array1 count]; i++){
   for (int j=0 ; j<[array2 count]; j++){
       objects = [[NSMutableArray alloc]init];
       objects=[array1 objectAtIndex:i][[array2 objectAtIndex:j]];
   }
}

2 个答案:

答案 0 :(得分:1)

如果我理解正确,您需要使用字典来提供所需的查找机制:

self.array1 = @[@"word", @"word2"].mutableCopy;
self.array2 = @[@"this goes with word1", @"this goes with the second word"].mutableCopy;

NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:self.array2
                                                       forKeys:self.array1];

NSLog(@"%@", dictionary[@"word"]);
// Prints "this goes with word1".

答案 1 :(得分:0)

使用它:

NSMutableArray *objects = [[NSMutableArray alloc]init];
for (int i=0 ; i<[array1 count]&&[array2 count]; i++){

    [objects addObject:[NSString stringWithFormat:@"%@%@",[array1 objectAtIndex:i],[array2 objectAtIndex:i]]];
}