gpt4 book ai didi

ios - 如何查找数组中的重复项?

转载 作者:行者123 更新时间:2023-11-30 14:11:31 24 4
gpt4 key购买 nike

我有两个数组,其中包含对象。每个对象都有一个属性“id”。如果 id 具有相同的值 - 它是重复的。如何通过匹配属性查找并删除重复项?

现在我使用这个,但有时它会丢失并将重复项写入数据库

 func checkForDupl() {

for var i = 0; i < JSONStorage.count; i++ {

for var b = 0; b < CDStorage.count; b++ {

if JSONStorage[i]!.id == CDStorage[b]!.id {

JSONStorage.removeAtIndex(i)
if JSONStorage.isEmpty {
return
}
}
}
}
}

最佳答案

我修改了你的函数,使其不再错过重复项

func checkForDupl() {
var i = 0
OUTER_LOOP: while i < JSONStorage.count {
for j in 0..<CDStorage.count {
if JSONStorage[i]!.id == CDStorage[j]!.id {
JSONStorage.removeAtIndex(i)
continue OUTER_LOOP
}
}
++i
}
}

注意:此函数仅从 JSONStorage 中删除 CDStorage 中存在的项目。它不会删除 JSONStorage

中的重复项

关于ios - 如何查找数组中的重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31705241/

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