我有以下两个数组。
NSArray *array1=[[NSArray alloc]initWithObjects:@"ABC",@"DEF", nil];
NSArray *array2=[[NSArray alloc]initWithObjects:@"ABC",@"123",@"DEF",@"DEF", nil];
现在我必须在 array2 中搜索每个 array1 的对象,并且需要获取匹配的索引。我的应用程序在array2 中包含一千多个对象。
请建议最好的方法,而不是将第二个 for 循环放在第一个 for 循环中
for (int i=0; i<array1.count; i++)
{
//Need to search the [array1 objectAtIndex:i] string in array2 and need to get the matched indexes into an array in best optimised way here.
NSMutableArray *matchedIndexesArray=[[NSMutableArray alloc]init];
NSString *stringToSearch=[array1 objectAtIndex:i];
//here i can put another array like below to get the matched indexes..but is there any optimized way other than this for loop here? or is there any simple inbuilt method to get the matched objects into an array here.
for (int j=0; j<array2.count; j++)
{
if ([stringToSearch isEqualToString:[array2 objectAtIndex:j]])
{
[matchedIndexesArray addObject:[NSString stringWithFormat:@"%d",j]];
}
}
NSLog(@"matchedIndexesArray-->%@<--",matchedIndexesArray);
//I will use this matchedIndexesArray here further processing...
//
//
//
//Large Code Here
//
//
//
}