从 CFArray 获取值时出错
Error with fetching value from CFArray
我正在尝试从地址簿中获取联系人。我的代码在 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。请帮忙
您不能将 UnsafePointer<Void>
强制转换为类型。您必须首先将该 void 指针转换为 UnsafePointer<Type>
然后获取其内存:
let aPerson = UnsafePointer<ABRecordRef>(CFArrayGetValueAtIndex(allPeople, i)).memory
仅供参考... ABAddressBook
已在 iOS 9 上弃用。对于针对 OS 的新代码,请改用 CNContactStore
。
我正在尝试从地址簿中获取联系人。我的代码在 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。请帮忙
您不能将 UnsafePointer<Void>
强制转换为类型。您必须首先将该 void 指针转换为 UnsafePointer<Type>
然后获取其内存:
let aPerson = UnsafePointer<ABRecordRef>(CFArrayGetValueAtIndex(allPeople, i)).memory
仅供参考... ABAddressBook
已在 iOS 9 上弃用。对于针对 OS 的新代码,请改用 CNContactStore
。