7

目前,我真的在为 ABAddressBookGetPersonWithRecordID 苦苦挣扎。我正在保存一个 ID,然后尝试再次调用它。目前我正在做一些简单的事情来测试链接,但它不起作用。

首先,我可以使用以下命令从我的 iphone 模拟器通讯录中读取对象:

-(BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    NSString *contactName;
    NSString *contactCompany;
    NSString *contactFirst;
    NSString *contactLast;


    contactFirst = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) stringByAppendingString:@" "];    
    contactLast =  (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    contactName = [contactFirst stringByAppendingString:contactLast];

    contactCompany = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);


    NSNumber *recordId = [NSNumber numberWithInteger: ABRecordGetRecordID(person)];

    NSLog(@"record id is %d", recordId);
    NSLog(@"Person Reference: %d", person);
    NSLog(@"Name: %@", contactName);
    NSLog(@"Company: %@", contactCompany);

哪个有 NSLog:

2010-01-26 11:52:31.396 SQL[19786:207] record id is 69283952
2010-01-26 11:52:31.397 SQL[19786:207] Person Reference: 69495792
2010-01-26 11:52:31.398 SQL[19786:207] Name: John Adams
2010-01-26 11:52:31.398 SQL[19786:207] Company: (null)

所以我的假设是这一切都是为了这个目的。问题是使用“记录 ID”69283952 来调用此联系信息备份。我目前正在尝试这样做:

-(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)newindexPath {


    ABAddressBookRef ab = ABAddressBookCreate();
    ABPersonViewController *pvc = [[ABPersonViewController alloc] init];

    ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,69283952);
    NSLog(@"person - %d", person);
    pvc.displayedPerson = person;
    NSLog(@"pvc.displayedPerson - %d", pvc.displayedPerson);
    pvc.addressBook = ab;
    pvc.allowsEditing = YES;
    pvc.personViewDelegate = self;
    [[self navigationController] pushViewController:pvc animated:YES];
    [pvc release];

哪个有 NSLog

2010-01-26 11:58:19.393 SQL[19849:207] Looking Up Contact Now
2010-01-26 11:58:19.399 SQL[19849:207] person - 0
2010-01-26 11:58:19.400 SQL[19849:207] pvc.displayedPerson - 0

因此,我得到的只是一个空的人。我究竟做错了什么?我完全不知道!

问候,@norskben

4

1 回答 1

7

NSNumber 是一个对象,而不是一个整数。

要将 NSNumber 对象插入格式字符串(例如 NSLog),您应该使用 %@(而不是 %d)。

NSNumber *recordId = [NSNumber numberWithInteger:ABRecordGetRecordID(person)];
NSLog(@"record id is %@",recordId);

同样,如果您在 NSNumber 对象中有 recordID,则可以使用以下方法获取整数值integerValue

ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,recordId.integerValue);
于 2010-01-26T11:37:27.117 回答