Swift3:ABSearchElement崩溃

时间:2016-09-15 12:03:30

标签: swift3 abaddressbook

我刚刚将Swift 2项目更新为Swift 3,而我在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]
}

发生此错误

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代码应该是什么?

1 个答案:

答案 0 :(得分:1)

将一些Optional传递给_SwiftValue时,经常会找到Any

尝试像这样更改comparisons

let comparisons = [firstNameSearch!, lastNameSearch!]