gpt4 book ai didi

Swift (iOS 8 SDK) 将 Unmanaged 转换为 ABMultiValueRef

转载 作者:IT王子 更新时间:2023-10-29 05:20:58 25 4
gpt4 key购买 nike

我需要从 AddressBook 框架转换这个函数的返回值:

ABRecordCopyValue(nil, kABPersonPhoneProperty)

到 ABMultiValueRef 类型的值

这个函数目前被标记为:

func ABRecordCopyValue(record: ABRecordRef!, property: ABPropertyID) -> Unmanaged<AnyObject>!

所以我可以像这样将它转换为非托管:

ABRecordCopyValue(person, kABPersonPhoneProperty) as Unmanaged<ABMultiValueRef>

但是我怎样才能将它作为 ABMultiValueRef 获取,以便我可以将它传递给这个函数?

func ABMultiValueGetIndexForIdentifier(multiValue: ABMultiValueRef!, identifier: ABMultiValueIdentifier) -> CFIndex

我这样做了:

let managedPhones = Unmanaged.fromOpaque(phones.toOpaque()).takeUnretainedValue() as ABMultiValueRef

而且我一直收到这个编译器错误:

Bitcast requires both operands to be pointer or neither
%89 = bitcast %objc_object* %88 to %PSs9AnyObject_, !dbg !325
LLVM ERROR: Broken function found, compilation aborted!
Command /Applications/Xcode6-Beta3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 1

最佳答案

我找到了解决方案:

func peoplePickerNavigationController(
peoplePicker: ABPeoplePickerNavigationController!,
didSelectPerson person: ABRecordRef!) {

/* Do we know which picker this is? */
if peoplePicker != personPicker{
return
}

/* Get all the phone numbers this user has */
let unmanagedPhones = ABRecordCopyValue(person, kABPersonPhoneProperty)
let phones: ABMultiValueRef =
Unmanaged.fromOpaque(unmanagedPhones.toOpaque()).takeUnretainedValue()
as NSObject as ABMultiValueRef

let countOfPhones = ABMultiValueGetCount(phones)

for index in 0..<countOfPhones{
let unmanagedPhone = ABMultiValueCopyValueAtIndex(phones, index)
let phone: String = Unmanaged.fromOpaque(
unmanagedPhone.toOpaque()).takeUnretainedValue() as NSObject as String

println(phone)
}
}

关于Swift (iOS 8 SDK) 将 Unmanaged<ABMultiValueRef> 转换为 ABMultiValueRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24678374/

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