在iOS中获取NSArray中匹配对象索引的最佳方法?

时间:2013-11-07 12:32:14

标签: ios objective-c nsarray

我有以下两个数组。

 NSArray *array1=[[NSArray alloc]initWithObjects:@"ABC",@"DEF", nil];
 NSArray *array2=[[NSArray alloc]initWithObjects:@"ABC",@"123",@"DEF",@"DEF", nil];

现在我必须搜索每个array1的对象和array2,并且需要获取匹配的索引。 我的应用程序在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
    //
    //
    //

}

3 个答案:

答案 0 :(得分:11)

根据NSSet文档,的成员资格测试比数组更快。 因此,将array1转换为第一个集合是有意义的:

NSSet *set1 = [NSSet setWithArray:array1];

然后测试array2的每个对象以获取该集合中的成员资格。这可以很方便 完成

NSIndexSet *matchingIndexes = [array2 indexesOfObjectsPassingTest:^BOOL(NSString *obj, NSUInteger idx, BOOL *stop) {
    return [set1 containsObject:obj];
}];

显示所有匹配的索引:

[matchingIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
    NSLog(@"%ld", (long)idx);
}];
// Output: 0, 2, 3

UPDATE:(问题编辑后)否,没有方法用NSArray填充匹配对象的索引。但是有一种方法可以填充NSIndexSetNSIndexSet是存储索引的特殊集合 进入其他一些数据结构,例如数组。然后你的代码看起来像

for (NSString *stringToSearch in array1) {
    NSIndexSet *matchingIndexes = [array2 indexesOfObjectsPassingTest:^BOOL(NSString *obj, NSUInteger idx, BOOL *stop) {
        return [stringToSearch isEqualToString:obj];
    }];

    NSLog(@"matchingIndexes: %@", matchingIndexes);

    // Work with matchingIndex, for example enumerate all indices:
    [matchingIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
        NSLog(@"%ld", (long)idx);
    }];
}

但我不知道它在性能方面是否有很大差异。

答案 1 :(得分:4)

NSArray *a = @[@"123", @"456", @"ABC", @"DEF"];
NSArray *b = @[@"123", @"ABC", @"---"];

NSIndexSet *indexes = [a indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop)
{
    return [b containsObject:obj]
}];

NSLog(@"%@", indexes);

答案 2 :(得分:0)

NSArray *array1=[[NSArray alloc]initWithObjects:@"ABC",@"DEF", nil];
    NSArray *array2=[[NSArray alloc]initWithObjects:@"ABC",@"123",@"DEF",@"DEF", nil];


    for (int i=0; i<array1.count; i++){
        for (int j=0; j<array2.count; j++) {
            if ([[array1 objectAtIndex:i] isEqualToString: [array2 objectAtIndex:j]]) {
                NSLog(@"Matched Indexes %d %@", i, [array1 objectAtIndex:i] );
            }
        }
       }