从CFArray获取值时出错

时间:2015-12-10 10:13:03

标签: swift unsafe-pointers abrecord cfarraybsearchvalues

我正在尝试从地址簿中获取联系人。我的代码在Objective C中正常工作,但当我将其转换为swift时,我遇到了错误

无法转换类型'Swift.UnsafePointer<()>'的值到'Swift.AnyObject'

以下是我的代码

    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
    }

如何将记录转换为ABRecordRef。请帮忙

1 个答案:

答案 0 :(得分:1)

您无法强制将UnsafePointer<Void>强制转换为某种类型。您必须先将该void指针转换为UnsafePointer<Type>然后获取其内存:

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

仅供参考... ABAddressBook已在iOS 9上弃用。对于定位该操作系统的新代码,请改用CNContactStore