NSMutableArray *noDup = [[NSMutableArray alloc]init];
NSMutableArray *dup = [[NSMutableArray alloc]init];

for (NSString *first in newsmall)
{
    BOOL hasfound = NO;
    //NSLog (@"first %@", first);

    for (NSString *second in newbig)
    {
        //NSLog (@"second %@", second);
        if ([second isEqualToString:first])
        {
            [dup addObject:first];
            hasfound = YES;
            break;
        }
    }

    if (!hasfound)
    {
        //NSLog (@"has not found %@", first);
        [noDup addObject:first];
    }
}

newsmall是一串只有字符串的小数组,而newbig是一串只有字符串的大数组。该应用程序自行关闭,没有任何调试警告。 NSLog显示“第一”和“第二”,但未显示“未找到”。怎么会?

最佳答案

哦,du。我现在了解您的问题。

颠倒比较数组的顺序。如果要查找newbig中不存在的字符串,请在newbig中进行迭代,同时查看newbig中存在的枚举单词。

代码看起来像这样(只有两行代码已更改):

NSMutableArray *noDup = [[NSMutableArray alloc]init];
NSMutableArray *dup = [[NSMutableArray alloc]init];

for (NSString *first in newbig)
{
    BOOL hasfound = NO;
    //NSLog (@"first %@", first);

    for (NSString *second in newsmall)
    {
        //NSLog (@"second %@", second);
        if ([second isEqualToString:first])
        {
            [dup addObject:first];
            hasfound = YES;
            break;
        }
    }

    if (!hasfound)
    {
        //NSLog (@"has not found %@", first);
        [noDup addObject:first];
    }
}

看到细微的差别了吗?

10-04 15:09