1

I am trying to create a code in Swift for OS X and one of its function is to searches for Address Book entries. The code bellow finds the entries and works fine with one search term, lets say person's name John. However, if second search term used: i.e. name and surname "John Doe" (space separated) it returns all the matches for the name "John" and all the matches for the surname "Doe".

How to force the code to search for the rest of the string i.e. surname only in the Address Book entries that already matched first part of the sting entered i.e. name?

Searching for John Dow I would like to get John Doe's Address Book entry only and not a list of "John Doe, John Roe, John Moe, Andrew Doe" etc?

Sorry for my English

let addressBook = ABAddressBook.sharedAddressBook()
var dataArray:[NSDictionary] = [["personCell": "", "emailCell": ""]]
var foundPeople = 0
var personNo = 0 

var searchString = "John Doe"


func searchAB() -> NSArray {

    var mySearchWord = searchString.componentsSeparatedByString(" ")

    for word in mySearchWord {

        var searchWord = mySearchWord.removeAtIndex(mySearchWord.endIndex.predecessor())
        let comparison: ABSearchConjunction = CFIndex(kABContainsSubStringCaseInsensitive.value)
        let firstNameSearch = ABPerson.searchElementForProperty(kABFirstNameProperty,
            label: nil,
            key: nil,
            value: searchWord,
            comparison: comparison)

        let middleNameSearch = ABPerson.searchElementForProperty(kABMiddleNameProperty,
            label: nil,
            key: nil,
            value: searchWord,
            comparison: comparison)

        let lastNameSearch = ABPerson.searchElementForProperty(kABLastNameProperty,
            label: nil,
            key: nil,
            value: searchWord,
            comparison: comparison)


        let comparisons = [firstNameSearch, middleNameSearch, lastNameSearch]
        let orComparison = ABSearchElement(forConjunction: CFIndex(kABSearchOr.value), children: comparisons)
        let found = addressBook.recordsMatchingSearchElement(orComparison) as! [ABRecord]


    for person in found {

        let firstName = person.valueForProperty(kABFirstNameProperty) as! String? ?? ""
        let middleName = person.valueForProperty(kABMiddleNameProperty) as! String? ?? ""
        let lastName = person.valueForProperty(kABLastNameProperty) as! String? ?? ""
        let emailsProperty = person.valueForProperty(kABEmailProperty) as! ABMultiValue?


        foundPeople++

       var tmpFirstName = firstName
       var booleanFName = tmpFirstName.isEqualToString(firstName)

        if let emails = emailsProperty {

            for i in 0..<emails.count() {

                let email = emails.valueAtIndex(i) as! String
                var emailType =  ABLocalizedPropertyOrLabel(emails.labelAtIndex(i)) //telefono numerio tipas
                if (booleanFName) {
                     personNo++
                    if (personNo == 1) { // first person

                        dataArray = [["personCell": "\(firstName) \(middleName) \(lastName)", "emailCell": "\(email) (\(emailType))"]]
                    } else {
                        dataArray += [["personCell": "\(firstName) \(middleName) \(lastName)", "emailCell": "\(email) (\(emailType))"]]

                    } // end if personNo
                    println("\n• \(personNo) \(firstName) \(middleName) \(lastName)")
                    println("\t\t\(email) (\(emailType))")

                    booleanFName = false
                } else {
                    dataArray += [["emailCell": "\(email) (\(emailType))"]]
                    println("\t\t\(email) (\(emailType))")
                } // end booleanFName
            } // end for i
          } // end if let emails
        } // end for person in found
     } // end for word

    return dataArray;
  } // end func
Munas
  • 111
  • 3

0 Answers0