0

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]];
   }
}
5
  • 1
    What are you trying to do on this line: objects=[array1 objectAtIndex:i][[array2 objectAtIndex:j]]; ? Commented Apr 5, 2014 at 7:19
  • Show where you create and populate these arrays (which don't appear related to each other from your code) Commented Apr 5, 2014 at 7:20
  • 1
    objects=[array1 objectAtIndex:i][[array2 objectAtIndex:j]]; <-what is this ? Commented Apr 5, 2014 at 7:22
  • I'm trying to create something so that when the word is referenced in the first array, the second prints the information. Commented Apr 5, 2014 at 7:39
  • 1
    So why don't you use a dictionary? Commented Apr 5, 2014 at 7:57

2 Answers 2

1

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".
Sign up to request clarification or add additional context in comments.

Comments

0

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]]];
}

1 Comment

Why should OP do that? Try to explain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.