1

我想从通讯录中检索出生日期并将其显示在表格视图单元格中。我可以从通讯录中检索出生日期,但是当我在表格视图单元格中显示它时,应用程序崩溃了。这是我的代码

// in viewDidLoad
 birthdateString = [ContactDetails getBirthDate:record];


 +(NSString*)getBirthDate:(ABRecordRef)record{

    NSString *birthdateString=[(NSDate*)ABRecordCopyValue(record, kABPersonBirthdayProperty) description];
    NSArray *array=[birthdateString componentsSeparatedByString:@" "];

    birthdateString=[array objectAtIndex:0] ;
    NSLog(@"birthDay: %@",birthdateString);
    return birthdateString;
}

// in cellForRowAtIndexPath
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CELL_ID] autorelease];
cell.detailTextLabel.text=birthdateString;

NSZombieEnabled 时的控制台日志

*** -[CFString isEqualToString:]: message sent to deallocated instance 0x6eb5520

任何帮助表示赞赏。

4

2 回答 2

1

它不是完整的代码,所以无法判断,但在调试期间检查 nil 值。有一些 NSString 没有获得内存分配。刚刚得到答案

// in cellForRowAtIndexPath
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2          reuseIdentifier:CELL_ID] autorelease];
  NSString *brithdateString=[self getBrithDate:record];
cell.detailTextLabel.text=birthdateString;

您正在使用 brithdateString ,它是本地的,在调用 getBrithDate 方法后被释放

于 2012-03-21T12:07:23.657 回答
0

试试这个

CFDateRef birthDate = ABRecordCopyValue(record, kABPersonBirthdayProperty);
NSDateFormatter *dateFormatter = nil;
if(birthDate)
{
   if (dateFormatter == nil) {
   dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
   [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
} 
NSString* birthDay = [dateFormatter stringFromDate:(NSDate*)birthDate ];
NSLog(@"birthDay: %@",birthDay);
if (dateFormatter != nil)
[dateFormatter release];
CFRelease(birthDate);
于 2012-03-21T12:51:44.713 回答