我试图使用peoplePicker访问姓名,电话等数据,我有以下功能:
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController, didSelectPerson person: ABRecordRef) {
let phone: ABMultiValueRef? = ABRecordCopyValue(person, kABPersonPhoneProperty)?.takeRetainedValue()
let name: ABMultiValueRef? = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue()
if (ABMultiValueGetCount(name) > 0) {
let indexName = 0 as CFIndex
let name = ABMultiValueCopyValueAtIndex(name, indexName).takeRetainedValue() as! String
print("name of selected contact = \(name)")
} else {
print("No name")
}
if (ABMultiValueGetCount(phone) > 0) {
let indexPhone = 0 as CFIndex
let phone = ABMultiValueCopyValueAtIndex(phone, indexPhone).takeRetainedValue() as! String
print("phone for selected contact = \(phone)")
} else {
print("No phone")
}
}
它适用于手机,但它给我一个联系人姓名为Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
的错误,为什么?
编辑:屏幕截图
SOLUTION:
let name = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue()
if (name as! String != "") {
print("name of selected contact = \(name)")
} else {
print("No name")
}
答案 0 :(得分:0)
只是一个假设。
ABMultiValueGetCount
确实接受ABMultiValueRef!
类型的参数,同时传递name
类型的ABMultiValueRef?
。
在Swift中这不是问题,但是这些功能被桥接到C,所以让我们尝试修复这种不一致。
请在这一行
let name: ABMultiValueRef? = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue()
将此?
替换为!
,如下所示:
let name: ABMultiValueRef! = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue()
让我知道它是否有效。