gpt4 book ai didi

objective-c - iOS简单循环有什么问题?

转载 作者:行者123 更新时间:2023-12-01 19:24:47 26 4
gpt4 key购买 nike

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

看到细微的差别了吗?

关于objective-c - iOS简单循环有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8285971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com