I have defined a struct as follows :
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
})
....
....
....
}}
Problem Statement : Need to filter an array of ECUnifiedStructure (ie . [ECUnifiedStructure]) based upon name and phone Number(check both substring and string as whole).
Current Implementation : The following is the current implementation I have done. Here both contacts and filter contacts are [ECUnifiedStructure].
self.filterContacts = self.contacts.filter {($0.contactName).range(of: textString, options: [ .caseInsensitive, .diacriticInsensitive ]) != nil} + self.contacts.filter {($0.contactNumber.compactMap {$0}.contains(textString))}
Issues with implementation : Getting result for filtering contact name as intended with results including both string as whole and substrings but
when filtering phone number , result for substrings not found . Only when we give whole phone number does the result turn up.
Probable cause :
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"]]]
current Result = empty.
Please propose an elegant way of doing it.If you can please point out what I did wrong . Thanks in advance.