gpt4 book ai didi

ios - 根据键值之一删除 CNContact 数组

转载 作者:行者123 更新时间:2023-11-28 16:04:58 26 4
gpt4 key购买 nike

我是一个敏捷的新手,正在尝试使用 Contacts 框架,但遇到了一个问题:-

我有一个名为 contactsCNContact 数组,键为 givenNamefamilyNamephoneNumbers 。我想从 contacts

数组中过滤掉定义为 String 类型的某些 names

我试过:

for name in namesToRemove { // 'name' is always in fullname format
contacts = contacts.filter () { $0.givenName + " " + $0.familyName != name }
}

但仅当同时指定了 givenNamefamilyName 时,删除才有效。

另外,请记住,在 iPhone 设备上,某些联系人的全名仅在其“名字”或“姓氏”列中指定。

我该怎么做?非常感谢!

最佳答案

您可以将 contains 用于 namesToRemove 数组,作为对 contacts 进行 filter 操作的条件:

let filteredContacts = contacts.filter { !namesToRemove.contains($0.givenName) }

我们首先设置一个CNContact 示例结构(因为您没有向我们提供最小的工作示例...)

/* example setup */
struct CNContact {
let givenName: String
let familyName: String
let phoneNumbers: [String]
init(_ givenName: String, _ familyName: String,
_ phoneNumbers: [String]) {
self.givenName = givenName
self.familyName = familyName
self.phoneNumbers = phoneNumbers
}
}

let contacts = [CNContact("David", "Scott", ["123-567", "010-111"]),
CNContact("Lisa", "Rowling", ["134-521", "121-731"]),
CNContact("Brad", "Knight", ["621-141", "551-723"]),
CNContact("David", "Phelps", ["652-723", "718-888"]),
CNContact("Sarah", "Bright", ["166-720", "378-743"])]

示例用法:

/* example #1: 
filter by removing any contacts that whose 'givenName' property
are contained in a list of given surnames to remove 'namesToRemove' */
let namesToRemove1 = ["David", "Sarah"]
let filteredContacts1 = contacts.filter { !namesToRemove1.contains($0.givenName) }

filteredContacts1.forEach {
print($0.givenName, $0.familyName, $0.phoneNumbers.first ?? "none")
} /* Lisa Rowling 134-521
Brad Knight 621-141 */

/* example #2:
filter by removing any contacts that whose 'givenName' and 'familyName'
properties are contained in a list of given full names to remove ('namesToRemove'),
where we know these full names are separated by exactly a single whitespace */
let namesToRemove2 = ["David Phelps", "Sarah Bright"]
let filteredContacts2 = contacts.filter {
!namesToRemove2.contains($0.givenName + " " + $0.familyName) }

filteredContacts2.forEach {
print($0.givenName, $0.familyName, $0.phoneNumbers.first ?? "none")
} /* David Scott 123-567
Lisa Rowling 134-521
Brad Knight 621-141 */

最后,基于您问题的后续更新的附加示例:

/* example #3: 
filter by removing any contacts where at least one of the following
holds:
1. 'givenName' + " " + 'familyName' equals a name in 'namesToRemove',
2. 'givenName' by itself equals a name in 'namesToRemove',
3. 'familyName' by itself equals a name in 'namesToRemove', */

let contacts2 = [CNContact("David", "Scott", ["123-567", "010-111"]),
CNContact("Lisa", "Rowling", ["134-521", "121-731"]),
CNContact("", "Brad Knight", ["621-141", "551-723"]),
CNContact("David Phelps", "", ["652-723", "718-888"]),
CNContact("Sarah", "Bright", ["166-720", "378-743"])]
print(" ")
let namesToRemove3 = ["David Phelps", "Sarah Bright", "Brad Knight"]
let filteredContacts3 = contacts2.filter {
!(namesToRemove3.contains($0.givenName + " " + $0.familyName) ||
namesToRemove3.contains($0.givenName) ||
namesToRemove3.contains($0.familyName)) }

filteredContacts3.forEach {
print($0.givenName, $0.familyName, $0.phoneNumbers.first ?? "none")
} /* David Scott 123-567
Lisa Rowling 134-521 */

关于ios - 根据键值之一删除 CNContact 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40419952/

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