微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我需要从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)
    }  
}

原文地址:https://www.jb51.cc/swift/319844.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐