gpt4 book ai didi

objective-c - 将 NSArray 项目与 NSArray 中的所有其他项目进行比较

转载 作者:行者123 更新时间:2023-11-28 19:13:36 24 4
gpt4 key购买 nike

我有一个 NSStrings 的 NSArray,我想知道如何将数组中的每个项目与数组中的每个其他项目进行比较,以查看是否有任何字符串与其他字符串不同。。 p>

我看过一个c++的例子

for (int i = 0; i < list.size(); i++) {
for (int j = i+1; j < list.size(); j++) {
// compare list.get(i) and list.get(j)
}
}

但是,如果在 Objective-C 中有更好更简单的方法,您是否会感到困惑?我还需要做的另一件事是确保该项目在循环时不会 self 比较。

任何帮助或示例将不胜感激。

UPDATE ** BOLD 是问题的更新部分 **

最佳答案

如果我没看错你的问题,你想要在列表中只出现一次的字符串,对吗?

NSCountedSet *counted = [NSCountedSet setWithArray:list];
for (NSString *string in counted) {
NSUInteger count = [counted countForObject:string];
if (count == 1) {
// process "string", it appears in the list just once
}
}

如果您只是想知道列表中是否有多个不同的值,请执行以下操作:

NSSet *set = [NSSet setWithArray:list];
if (set.count == 1) {
// There is only one distinct value in the list
} else {
// There is more than one distinct value in the list
}

关于objective-c - 将 NSArray 项目与 NSArray 中的所有其他项目进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13635811/

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