gpt4 book ai didi

ios - 从包含字符串数组的对象元素中过滤结构对象

转载 作者:行者123 更新时间:2023-11-28 13:48:46 25 4
gpt4 key购买 nike

我定义了一个结构如下:

struct ECUnifiedStructure{

var contactName : String!
var contactNumber = [String]()
...
...

init(contact: CNContact , EProfile : Bool) {
let validTypes = [
CNLabelPhoneNumberiPhone,
CNLabelPhoneNumberMobile,
CNLabelPhoneNumberMain,
CNLabelHome,
CNLabelWork
]

var givenName = contact.givenName + " " + contact.middleName
let familyName = contact.familyName
if (givenName == "" && familyName == "") || givenName == " "{
givenName = contact.organizationName
}

self.contactName = givenName.capitalized + familyName.capitalized
self.contactNumber = contact.phoneNumbers.compactMap({ (PhoneNumber) -> String? in
if let phoneLabel = PhoneNumber.label , validTypes.contains(phoneLabel){
return PhoneNumber.value.stringValue.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "-", with: "")
}
return nil
})
....
....
....
}}

问题陈述:需要根据姓名和电话号码过滤 ECUnifiedStructure 数组(即 [ECUnifiedStructure])(检查整个子字符串和字符串)。

当前实现: 以下是我目前完成的实现。这里的联系人和过滤器联系人都是[ECUnifiedStructure]。

self.filterContacts = self.contacts.filter {($0.contactName).range(of: textString, options: [ .caseInsensitive, .diacriticInsensitive ]) != nil} + self.contacts.filter {($0.contactNumber.compactMap {$0}.contains(textString))}

实现问题:按预期获取过滤联系人姓名的结果,结果包括整个字符串和子字符串,但

when filtering phone number , result for substrings not found . Only when we give whole phone number does the result turn up.

可能的原因:

self.contacts.filter {($0.contactNumber.compactMap {$0}.contains(textString))}

Eg : Suppose contacts = [[name: "David" , phoneNumber : ["1234567890",9876543210]], [name: "Hilton" , phoneNumber : ["1011111111","2222222222"]] , [name: "lewis" , phoneNumber : ["1111111111","2222222222"]]]

searchString = "10"

intended result = [[name: "David" , phoneNumber : ["1234567890",9876543210]], [name: "Hilton" , phoneNumber : ["1011111111","2222222222"]]]

当前结果 = 空。

请提出一个优雅的方法。如果可以请指出我做错了什么。提前致谢。

最佳答案

这将返回任何电话号码包含“10”的结构对象

let result = contacts.filter{ $0.phoneNumber.contains(where: { $0.contains("10")}) }

使用你原来的过滤器代码

self.filterContacts = self.contacts.filter {($0.contactName).range(of: textString, options: [ .caseInsensitive, .diacriticInsensitive ]) != nil} + self.contacts.filter { $0.contactNumber.contains(where: {$0.contains(textString)})}

这是我完整的测试代码

struct ECUnifiedStructure{
var name : String!
var phoneNumber = [String]()
}

let contacts:[ECUnifiedStructure] = [ECUnifiedStructure(name: "David" , phoneNumber : ["1234567890","9876543210"]),
ECUnifiedStructure(name: "Hilton" , phoneNumber : ["1011111111","2222222222"]) ,
ECUnifiedStructure(name: "lewis" , phoneNumber : ["1111111111","2222222222"])]

let result = contacts.filter{ $0.phoneNumber.contains(where: { $0.contains("10")}) }

for r in result {
print(r)
}

输出

ECUnifiedStructure(name: Optional("David"), phoneNumber: ["1234567890", "9876543210"])
ECUnifiedStructure(name: Optional("Hilton"), phoneNumber: ["1011111111", "2222222222"])

关于ios - 从包含字符串数组的对象元素中过滤结构对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55040388/

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