0

I have just updated my Swift 2 project to Swift 3 and I'm having a problem with a query on the AddressBook:

import Cocoa
import AddressBook

let firstName:String = "John"
let lastName:String = "Appleseed"

let addressBook = ABAddressBook.shared()
let firstNameSearch = ABPerson.searchElement(forProperty:    kABFirstNameProperty,
                                         label: nil,
                                         key: nil,
                                         value: firstName,
                                         comparison:    ABSearchComparison(kABEqualCaseInsensitive.rawValue))
let lastNameSearch = ABPerson.searchElement(forProperty: kABLastNameProperty,
                                        label: nil,
                                        key: nil,
                                        value: lastName,
                                        comparison: ABSearchComparison(kABEqualCaseInsensitive.rawValue))


let comparisons = [firstNameSearch, lastNameSearch]
let andComparison = ABSearchElement(forConjunction: CFIndex(kABSearchAnd.rawValue), children: comparisons)
let peopleFound = addressBook?.records(matching: andComparison) as! [ABRecord]
if peopleFound.count > 0
{
   let contact = peopleFound[0]
}

It's crashing with this error

2016-09-15 12:59:02.657 com.apple.dt.Xcode.PlaygroundStub-macosx[37940:8204350] -[_SwiftValue searchRecordClasses]: unrecognized selector sent to instance 0x7fc098ec9600 2016-09-15 12:59:02.658 com.apple.dt.Xcode.PlaygroundStub-macosx[37940:8204350] An uncaught exception was raised 2016-09-15 12:59:02.658 com.apple.dt.Xcode.PlaygroundStub-macosx[37940:8204350] -[_SwiftValue searchRecordClasses]: unrecognized selector sent to instance 0x7fc098ec9600

when executing this line:

let andComparison = ABSearchElement(forConjunction: CFIndex(kABSearchAnd.rawValue), children: comparisons)

Does anyone know what the updated Swift 3 code should be?

iphaaw
  • 6,764
  • 11
  • 58
  • 83

1 Answers1

1

That _SwiftValue is often found when passing some Optional to Any.

Try changing your comparisons like this:

let comparisons = [firstNameSearch!, lastNameSearch!]
OOPer
  • 47,149
  • 6
  • 107
  • 142