Swift:在数组中查找重复的CNContact对象

编程入门 行业动态 更新时间:2024-10-28 14:23:57
本文介绍了Swift:在数组中查找重复的CNContact对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在 Array 内获取重复的 CNContacts :

I am trying to get duplicate CNContacts inside an Array:

代码:

func fetchDuplicateContacts(){ self.didFinished_getDuplicateContacts = false; self.arr_duplicateContacts.removeAllObjects() NSLog("Start fetch duplicate", "") self.duplicateContactsCount = 0; for (var i: Int = 0; i < self.arr_allContacts.count; i++){ let contact1 = self.arr_allContacts[i] var hasDuplicate: Bool = false let arr_childs: NSMutableArray = NSMutableArray() for (var j: Int = 0; j < self.arr_allContacts.count; j++){ let contact2 = self.arr_allContacts[j] if(contact1 != contact2){ if CNContactFormatter.stringFromContact(contact1, style: .FullName) == CNContactFormatter.stringFromContact(contact2, style: .FullName){ if(self.checkIfContactsHaveSamePhones(contact1, contact2: contact2)){ print("Move on cuz duplicate: \(CNContactFormatter.stringFromContact(contact1, style: .FullName))") duplicateContactsCount += 1; arr_childs.addObject(NSDictionary(objects: [contact2, false], forKeys: ["object", "checked"])) hasDuplicate = true } } } } // This is for adding first contact to main array to be the first. It's important to be like this if hasDuplicate == true{ arr_childs.insertObject(NSDictionary(objects: [contact1, false], forKeys: ["object", "checked"]), atIndex: 0) var key: NSString? = CNContactFormatter.stringFromContact(contact1, style: .FullName) if key == nil { key = "no name \(i)" } arr_duplicateContacts.addObject([key! : arr_childs]) } } NSLog("End fetch duplicate w results \(self.duplicateContactsCount)", "") self.didFinished_getDuplicateContacts = true; dispatch_async(dispatch_get_main_queue(), { () -> Void in self.mytable.reloadData() }) }

我循环遍历 array ,检查每个2个联系人是否具有相同的名称和编号,如果 true ,则将它们添加到 Array NSDictionary 的代码(其键是联系人姓名,而对象是 NSDictionary ,其中包含 CNContact 和一个布尔值"checked").凌乱,我知道.

I loop trough the array, check each 2 contacts if have same name and same numbers and if true, add them to an Array of NSDictionary (whose key is contact name, and object is an NSDictionary which contain CNContact and a bool "checked").. A little bit messy, I know.

*问题:我将在主数组中重复*

假设我有 Contact1 ["bob","07100"] , Contact2 ["bob","07100"] .当"j"循环将检查 Contact1 == Contact1 是否为true,并跳过将对象添加到数组中,然后检查 Contact1 == Contact2 (false,然后看到 Contact2 是重复的,并且在主数组中有广告).在 i ++ 之后,它对 Contact2 做同样的事情,这就是问题(如果不清楚,请尝试找出3个对象).

Let's say that I have Contact1["bob", "07100"], Contact2["bob","07100"]. When "j" loop will check if Contact1 == Contact1 which is true and skips adding object to array and after that checks Contact1 == Contact2(false and then it sees that Contact2 is a duplicate and ads to main array). After i++ it does same thing with Contact2 and this is the problem (try figure out for 3 objects if is not clear).

尝试解决此问题,方法是假设重复的联系人是一个接一个的联系人,并使用 i = j 但使用 if Contact1 == Contact3 , Contact2 将从验证中跳过

Tried solving it by asuming that duplicate contacts are one after another and used that i = j but if Contact1 == Contact3, Contact2 will be skiped from verification

有什么办法解决这个问题吗?

Any idea how to solve this problem?

推荐答案

我想我会提出另一种方法,以利用Swift的某些内置函数来实现您要执行的操作.关于此解决方案要注意的一件事是,它不会帮助您确定哪个是重复项,哪个是用户想要保留的联系人.这实际上是您的方法存在的问题,因为如果两个联系人的姓名相同但号码不同,会发生什么情况?这就是为什么在我的解决方案中,我会将重复项归为一组,然后您(或您的用户)可以决定要做什么.

I thought I would propose another way of achieving what you are trying to do leveraging some of Swift's built in functions. One thing to note about this solution is that it will not help you determine which is the duplicate and which is the contact the user wants to keep. This is actually an issue with your approach because what happens if both contacts have the same name but a different number? This is why in my solution I group the duplicates and you (or your user) can decide what to do.

  • 获取所有全名的数组:

  • Get an array of the all the full names: let fullNames = self.arr_allContacts.map(CNContactFormatter.stringFromContact($0, style: .FullName))

  • 使该数组唯一

  • Make that array unique

    let uniqueArray = Array(Set(fullNames))

  • 这是我将要执行的步骤与您正在执行的步骤不同的步骤.我将构建一个数组数组,因为我认为它将到达您想要去的地方.

  • This is the step that I will be doing differently than what you are doing. I will build an array of arrays because I think it will get to where you want to go.

    var contactGroupedByUnique = [Array]() for (fullName in uniqueArray) { var group = self.arr_allContacts.filter { CNContactFormatter.stringFromContact($0, style: .FullName) == fullName } contactGroupedByUnique.append(group) }

  • 现在您可以执行以下操作:

  • Now you can do the following:

    contactGroupedByUnique.count = //number of unique contact contactGroupedByUnique[index] = //number of duplicates of that contact

  • 我没有时间测试代码,但这应该可以帮助您.

    I don't have time to test the code but this should get you there.

    更多推荐

    Swift:在数组中查找重复的CNContact对象

    本文发布于:2023-11-22 00:58:06,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1615348.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:组中   对象   Swift   CNContact

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!