0

我正在尝试从通讯录中获取联系人。我的代码在 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。请帮忙

4

1 回答 1

1

您不能强制将 an 强制UnsafePointer<Void>转换为类型。您必须首先将该 void 指针转换为UnsafePointer<Type>然后获取其内存:

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

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

于 2015-12-10T11:25:19.247 回答