How to display the data when one object is zero but not the other?

advertisements

I have a tableView, similar to Contacts. The user can add his contacts in it and gets the names and the number displayed. When a contact only has a firstname and not a lastname he display the firstname and (null). In order to avoid it, I use an if-statement. But for some reason I always get a SIGABRT error at the line cell.textLabel.text = value;.

Here is the code to obtain the data:

- (BOOL)peoplePickerNavigationController:

(ABPeoplePickerNavigationController *)peoplePicker

  shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

    ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
    number = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
    [thenumbers addObject:number];

if(lastName && ([lastName length] > 0)) {
[menuArray addObject:[[Contacts alloc] initWithFirstName:firstName andLastName:lastName]];
   } else {
       [menuArray addObject:firstName];}

    [self dismissModalViewControllerAnimated:YES];
    return NO;
    }

And here the code to display it:

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:CellIdentifier];}
if(lastName && ([lastName length] > 0)) {
    Contacts *user = [menuArray objectAtIndex:indexPath.row];
    NSString *cellValue = [NSString stringWithFormat:@"%@ %@", [user firstName], [user lastName]];
    cell.textLabel.text = cellValue;
}  else {
    NSString *value = [menuArray objectAtIndex:indexPath.row];
    cell.textLabel.text = value;

}

NSString *numbers = [thenumbers objectAtIndex:indexPath.row];
cell.detailTextLabel.text = numbers;

return cell;
}


SIGABRT means an exception was thrown. Look in the console log for the actual exception. I suspect you're actually getting it on the previous line:

    NSString *value = [menuArray objectAtIndex:indexPath.row];

The most likely error is that indexPath.row is past the end of menuArray.


EDIT: I think I see your error. It's actually probably a "does not respond to selector" error. When you build your menuArray, sometimes you add a Contacts and sometimes you add an NSString. (Contacts is a very strange name of this object; it seems to be a single person.) You never clear the old menuArray, and I suspect when you change lastName that you are winding up with stale information in there that then has a type-conflict later.

Also note that you're leaking lastName and firstName. You need __bridge_transfer here, not __bridge.