0

I'm trying to fetch contacts from address book. My code is working properly in Objective C, but when I converted it to swift I'm getting error

Could not cast value of type 'Swift.UnsafePointer<()>' to 'Swift.AnyObject'

Below is my code

    var errorRef: Unmanaged<CFError>? = nil
    let addressBook: ABAddressBookRef = ABAddressBookCreateWithOptions(nil, &errorRef).takeRetainedValue()
    let allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray //as [ABRecordRef]
    let numberOfPeople = ABAddressBookGetPersonCount(addressBook)

    for (var i=0; i < numberOfPeople; i++){
        runOnMainQueueWithoutDeadlocking({ () -> () in
            ProgressHUD.show("Reading Contact \(i) out of \(numberOfPeople)")
        })

        let aPerson = CFArrayGetValueAtIndex(allPeople, i) as! ABRecordRef     //Error here
    }

How can I convert record to ABRecordRef. Please help

Ankita Shah
  • 2,178
  • 3
  • 25
  • 42

1 Answers1

1

You cannot force cast an UnsafePointer<Void> to a type. You must first convert that void pointer to UnsafePointer<Type> then take its memory:

let aPerson = UnsafePointer<ABRecordRef>(CFArrayGetValueAtIndex(allPeople, i)).memory

FYI... ABAddressBook has been deprecated on iOS 9. For new code targeting that OS, use CNContactStore instead.

Code Different
  • 90,614
  • 16
  • 144
  • 163