Swift (iOS 8 SDK) Convert Unmanaged<ABMultiValueRef> to ABMultiValueRef

依然范特西╮ 提交于 2019-11-30 00:30:56
Vandad Nahavandipoor

I found the solution:

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)
    }  
}

In case someone is looking for a final way to deal with ABRecords in Swift 2, here it is:

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController, didSelectPerson person: ABRecord) {

    let firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty).takeRetainedValue()
    let lastName = ABRecordCopyValue(person, kABPersonLastNameProperty).takeRetainedValue()

    var emails:[String] = []
    let emailRecords = ABRecordCopyValue(person, kABPersonEmailProperty).takeRetainedValue() as ABMultiValueRef
    let emailsCount = ABMultiValueGetCount(emailRecords)
    for index in 0 ..< emailsCount {
        if let email = ABMultiValueCopyValueAtIndex(emailRecords, index).takeRetainedValue() as? String {
            emails.append(email)
        }
    }

    print("Contact selected. firstName: \(firstName), lastName: \(lastName), emails: \(emails)")

}
marchiore

Same with me, this probably is a compiler problem. if you want to follow my thread too ABPeoplePickerNavigationController shouldContinueAfterSelectingPerson deprecated Swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!