How can I compare two NSArrays so that I can delete everything that isn’t in both arrays.
I tried it like this:
NSArray *array1 = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"1",@"2",@"5",@"6", nil];
NSMutableArray *myMutableArray = [NSMutableArray arrayWithArray:array1];
NSMutableArray *myMutableArrayTwo = [myMutableArray copy];
[myMutableArray removeObjectsInArray:array2];
[array1 release];
[array2 release];
NSArray *array3 = [myMutableArray copy];
[myMutableArrayTwo removeObjectsInArray:array3]; // Error here: "SIGABRT"
NSLog(@"array3:%@",myMutableArrayTwo);
But it doesn’t work because of the error. It sais: “-[__NSArrayI removeObjectsInArray:]: unrecognized selector sent to instance 0x4e51550”
What did I do wrong? Or are there easier ways to solve my problem?
Thanks for the help
This is because you create a copy (non-mutable) of a NSMutableArray, so you are losing the mutable capabilities.
Use the
mutableCopymethod instead.By the way, you’ve got huge memory problems. Your
array1andarray2variables are auto-released objects. Releasing them will lead you to problems.You only need to release the arrays you created with
alloc,copyormutableCopy.