【问题标题】:iOS Address Book error on ABMultiValueRefABMultiValueRef 上的 iOS 通讯簿错误
【发布时间】:2011-08-12 14:00:10
【问题描述】:

我在访问我的 iPad 2 的地址簿时遇到了问题。尤其是我在检索我的联系人的电子邮件时遇到了问题。我想做的是访问通讯录,检索我的联系人并在表格视图中显示它们。由于显示了联系人的姓名和姓氏,因此一切似乎都很好。问题出在电子邮件属性上,因为当我尝试检索它时,我得到一个“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);

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

提前致谢

【问题讨论】:

  • 我也遇到了同样的问题。你发现原因了吗?

标签: iphone ios addressbook core-foundation multivalue


【解决方案1】:
ABMultiValueRef mails = ABRecordCopyValue([self.contacts objectAtIndex:index], kABPersonEmailProperty);

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

检查框架和 self.contacts。

我的应用使用两个框架。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 2014-08-31
    • 2013-03-07
    • 2017-04-20
    • 2010-09-07
    相关资源
    最近更新 更多