2011-08-12 50 views
1

我在访问iPad 2的地址簿时遇到问题。特别是在检索我的联系人电子邮件时遇到问题。我想要做的就是访问地址簿,检索我的联系人并在表格视图中显示他们。因为显示联系人的名字和姓氏,所以一切看起来都很好。问题是电子邮件属性,因为当我尝试检索它时,我得到一个“EXC_BAD_ACCESS”。 我写来显示tableview中记录的代码如下:ABMultiValueRef上的iOS地址簿错误

- (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,但我不明白为什么。 我希望有人能帮助我。

在此先感谢

+1

我有这个相同的问题。你有没有发现原因? – DexterW

回答

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

它工作正常,在我的应用程序。

检查框架& self.contacts。

我的应用程序使用两个框架。

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