Question

I'm creating an array from two mutable arrays. Array 1 has 3 objects and so does array 2 I'm receiving a crash that says "unrecognized selector sent to instance'" Why doesn't this work?

The two arrays are initialised:

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]];
   }
}
Was it helpful?

Solution

If I understand you correctly you need to use a dictionary to provide the sort of lookup mechanism you want:

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

OTHER TIPS

use this:

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]]];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top