Reputation: 69
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]];
}
}
Upvotes: 0
Views: 290
Reputation:
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".
Upvotes: 1
Reputation: 166
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]]];
}
Upvotes: 0