本文介绍了Swift (iOS 8 SDK) 转换非托管<ABMultiValueRef>到 ABMultiValueRef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to convert the return value of this function from the AddressBook framework:

ABRecordCopyValue(nil, kABPersonPhoneProperty)

到 ABMultiValueRef 类型的值

to a value of type ABMultiValueRef

此功能目前标记为:

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

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

So I can convert it to Unmanaged like so:

ABRecordCopyValue(person, kABPersonPhoneProperty) as Unmanaged<ABMultiValueRef>

但是我怎样才能把它作为一个 ABMultiValueRef 来传递给这个函数呢?

But then how can I get it as an ABMultiValueRef so that I can pass it to this function?

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

我是这样做的:

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

而且我不断收到此编译器错误:

And I keep getting this compiler error:

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) 转换非托管<ABMultiValueRef>到 ABMultiValueRef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 23:48