iPhone通讯录 - 在ABAddressBookGetPersonWithRecordID中获取空项目

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
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);

有趣的是,recordId是一个相对较小的数字(我得到了2),并且与旧的67238032完全不同。这就是解决问题的方法!非常感谢gerry3。 - oberbaum
是的,69283952是您的NSNumber对象的指针值(内存地址)。 - gerry3
我了解“指针”的感觉。我也有同样的问题...但是随着使用它们的时间增加,这个问题会消失。 :) - Jann

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接