简体   繁体   English

将ABRecordID始终转换为NSNumber -1

[英]Convert ABRecordID to NSNumber always -1

I am trying to save an ABRecordID of an existing contact from the Address Book into Core Data. 我正在尝试将现有联系人的ABRecordID从通讯簿保存到核心数据中。 I have an Int32 defined in my model, but in order to save my object there I need to convert the ABRecordID (which is an Int32) into an NSNumber 我在模型中定义了一个Int32,但是为了保存我的对象,我需要将ABRecordID(这是一个Int32)转换为NSNumber

Here is my code that converts it: 这是我将其转换的代码:

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
    let multiValue: ABMultiValueRef = ABRecordCopyValue(person, property).takeRetainedValue()
    let index = ABMultiValueGetIndexForIdentifier(multiValue, identifier)
    let pickedVal = ABMultiValueCopyValueAtIndex(multiValue, index).takeRetainedValue() as! String

    let theRecord = recordIdFromPersonRecord(person)
    println(ABRecordGetRecordID(person!))

func recordIdFromPersonRecord (personRecord: ABRecord) -> NSNumber {
    let recordId = ABRecordGetRecordID(personRecord)
    println(personRecord)
    return (NSNumber(int: recordId))
}

This code always prints -1 in the console, no matter what record I select in my address book. 无论我在通讯簿中选择什么记录,此代码始终在控制台中显示-1。 I'm a newbie programmer and I'm totally stuck. 我是一个新手程序员,我完全陷入困境。

Thanks for your help! 谢谢你的帮助!

EDIT: Commenter below indicated that this is a value reserved for records that do not exist in the address book database... 编辑:下面的注释者指出,这是为通讯录数据库中不存在的记录保留的值...

That is confusing. 这很令人困惑。 I am just browsing the AB to get the name value and the ID out of existing records... how can existing records not have a record saved to the address book database? 我只是在浏览AB以从现有记录中获取名称值和ID ...现有记录如何不能将记录保存到地址簿数据库中? Here is what the definition from the docs is: Records with this ID have not been saved to the Address Book database. 这是docs中的定义:具有该ID的记录尚未保存到通讯簿数据库中。 Am I misunderstanding? 我误会了吗

In iOS 8, you can browse the contacts without it ever requesting permission to the address book (because you might, for example, allow the user to perform the default action with ever returning actual data to the app). 在iOS 8中,您可以浏览联系人而无需请求通讯录的权限(例如,因为您可能允许用户执行默认操作,并将实际数据返回给应用程序)。 But to actually get the particulars on a contact, you have to explicitly request permission to do so. 但是要实际获取联系人的详细信息,您必须明确请求权限。

It would appear that you must have used AddressBookUI.framework to present the contact UI, but never asked for permission to access the particulars about the contact. 看来您必须使用AddressBookUI.framework来显示联系人UI,但从未要求获得访问该联系人详细信息的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM