Swift3:ABSearchElement 崩溃

Swift3: ABSearchElement crash

我刚刚将我的 Swift 2 项目更新为 Swift 3,我在地址簿上的查询有问题:

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]
}

它因这个错误而崩溃

2016-09-15 12:59:02.657 com.apple.dt.Xcode.PlaygroundStub-macosx[37940:8204350] -[_SwiftValue searchRecordClasses]:无法识别的选择器发送到实例 0x7fc098ec9600 2016-09-15 12:59:02.658 com.apple.dt.Xcode.PlaygroundStub-macosx[37940:8204350] 引发了未捕获的异常 2016-09-15 12:59:02.658 com.apple.dt.Xcode.PlaygroundStub-macosx[37940:8204350] -[_SwiftValue searchRecordClasses]:无法识别的选择器发送到实例 0x7fc098ec9600

执行此行时:

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

有谁知道更新后的 Swift 3 代码应该是什么?

_SwiftValue 经常在将一些 Optional 传递给 Any 时发现。

尝试像这样更改您的 comparisons

let comparisons = [firstNameSearch!, lastNameSearch!]