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

3

14

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

ABRecordCopyValue(nil, kABPersonPhoneProperty)

to a value of type ABMultiValueRef

This function is currently marked as this:

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

So I can convert it to Unmanaged like so:

ABRecordCopyValue(person, kABPersonPhoneProperty) as Unmanaged<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

I did this:

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
Coen answered 10/7, 2014 at 13:52 Comment(4)
I have the same error. I am stuck with fetching book records sorted :(Marijuana
I have heard that this is because Apple has not yet (as of iOS 8 SDK Beta 4) enabled CF_IMPLICIT_BRIDGING_ENABLED on AddressBook framework.Coen
Ok, Now I understand :(Marijuana
I posted my solution now. You cannot say as ABMultiValueRef. You should say "as NSObject as ABMultiValueRef". I hope this helpsCoen
C
25

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)
    }  
}
Coen answered 26/7, 2014 at 14:1 Comment(2)
Rather than the fromOpaque/toOpaque dance, why not just do let phones: ABMultiValueRef = unmanagedPhones.takeUnretainedValue() and let phone = unmanagedPhone.takeUnretainedValue() as String?Zasuwa
This is a Beta 5 fix. Apple has fixed this now so no need to do all the Unmanaged business.Coen
S
1

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

}
Stated answered 14/7, 2016 at 10:57 Comment(1)
Such a life saver!Overreact
E
0

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

Ehrman answered 21/7, 2014 at 21:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.