iphone - ABMultiValueRef 上的 iOS 地址簿错误

标签 iphone ios addressbook core-foundation multivalue

我在访问 iPad 2 的地址簿时遇到问题。尤其是在检索联系人的电子邮件时遇到问题。我想要做的是访问地址簿,检索我的联系人并在表格 View 中显示它们。由于显示了联系人的姓名,因此一切似乎都正常。问题出在电子邮件属性上,因为当我尝试检索它时,我得到了“EXC_BAD_ACCESS”。 我编写的用于显示 tableview 记录的代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *tableIdentifier = @"tableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableIdentifier] autorelease];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];

    NSUInteger row = [indexPath row];

    NSString *firstName = (NSString *)ABRecordCopyValue([contacts objectAtIndex:row], kABPersonFirstNameProperty);
    NSString *lastName = (NSString *)ABRecordCopyValue([contacts objectAtIndex:row], kABPersonLastNameProperty);
    NSString *name = [[NSString alloc] initWithFormat:@"%@ %@", lastName,firstName];

    [firstName release];
    [lastName release];

    cell.textLabel.text = name;

    [name release];

    NSArray *emails = [[self getEmailForPerson:row] retain];

    /*......*/  

    return cell;
}

虽然获取我的联系人的电子邮件的功能如下:

- (NSArray *)getEmailForPerson:(NSInteger)index{
    //Create the array where emails will be stored
    NSMutableArray *m = [[[NSMutableArray alloc] init] autorelease];
    //Get the email properties
    ABMultiValueRef mails = ABRecordCopyValue([self.contacts objectAtIndex:index], kABPersonEmailProperty);
    //Iterate in the multi-value properties
    for (int i=0; i<ABMultiValueGetCount(mails); i++) {
        //Get the email
        NSString *mail = (NSString *) ABMultiValueCopyValueAtIndex(mails, i);
        //Add the email to the array previously initializated
        [m addObject:mail];
        [mail release];
    }
    CFRelease(mails);

    return m; 
}

当我在这条语句之后运行调试器时

ABMultiValueRef mails = ABRecordCopyValue([self.contacts objectAtIndex:index], kABPersonEmailProperty);

邮件似乎没有初始化,因为它的地址是 0x0,但我不明白为什么。 我希望有人能帮助我。

提前致谢

最佳答案

ABMultiValueRef mails = ABRecordCopyValue([self.contacts objectAtIndex:index], kABPersonEmailProperty);

它在我的应用中运行良好。

检查框架和 self.contacts。

我的应用使用两个框架。

#import <AddressBook/AddressBook.h>
#import <AddressBook/ABAddressBook.h>

关于iphone - ABMultiValueRef 上的 iOS 地址簿错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7041170/

相关文章:

iOS 应用程序在模拟器中运行,启动时发生临时分发崩溃

ios - UITableView 禁用用户不可见的行

iphone - 创建基于文本内容的 iPhone/iPad 应用程序

ios - 构建代码的最佳方式

ios - 我需要两个计时器在同一个 View Controller ios 中工作

iphone - 自定义 ABPeoplePickerNavigationController

ios - 地址簿 iOS : Contact name from string

ios - 从 iOS 地址簿 API 查找常用联系人

ios - UIScrollView inside UIScrollView – 在 TextView 范围内忽略 ScrollView 触摸?

iphone - 如何将参数从 NSTimer 目标传递给自定义选择器?