gpt4 book ai didi

ios - Swift - 从 peoplePickerNavigationController 访问名称

转载 作者:行者123 更新时间:2023-11-30 13:42:07 27 4
gpt4 key购买 nike

我正在尝试使用 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")
}
}

它适用于手机,但给我一个联系人姓名错误线程 1:EXC_BAD_ACCESS (code=EXC_I386_GPFLT),为什么?

编辑:屏幕截图

screen

解决方案:

let name = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue()

if (name as! String != "") {

print("name of selected contact = \(name)")
} else {
print("No name")
}

最佳答案

只是一个假设。

ABMultiValueGetCount接受 ABMultiValueRef! 类型的参数当你经过它时name有这种类型 ABMultiValueRef? .

在 Swift 中这不是问题,但这些函数已桥接到 C,因此让我们尝试修复此不一致问题。

请在此行

let name: ABMultiValueRef? = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue()

替换?有了这个! ,如下所示:

let name: ABMultiValueRef! = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue()

请告诉我它是否有效。

关于ios - Swift - 从 peoplePickerNavigationController 访问名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35434989/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com