iphone地址簿 - 在ABAddressBookGetPersonWithRecordID中获取null项

时间:2010-01-26 11:01:29

标签: iphone objective-c addressbook

此刻我正在努力克服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 个答案:

答案 0 :(得分: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);