Question

I want to retrieve the birth date from address book & display it in table view cell. I can retrieve the birth date from address book but when I show it in table view cell , app crashes. Here is my code

// 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;

Console log when NSZombieEnabled

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

Any kind help is appreciated.

Était-ce utile?

La solution

Its not the full code so cant tell , but check for nil value during debugging . There is some NSString which is not getting memory allocation. Just got the answer

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

You are using brithdateString which was local and deallocated after the getBrithDate method was called

Autres conseils

try this

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);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top