uitableviewios5nsstringabpeoplepickerview

NSString has null value when concatenated


I have a UITableView with multiple sections. A section of the tableview has 2 rows, of which one is editable(insert button) and other displays a name from addressbook. On the click of the insert button in the cell, I am loading peoplePickerView and I select a contact.

I get the contact from addressbook as

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

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

    NSString *middleName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonMiddleNameProperty);

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

    self.contactName = [NSString stringWithFormat:@"%@/%@/%@", firstName ?: @"", middleName ?: @"", lastName ?: @""];

    [self.myTableView reloadData];
    [self dismissViewControllerAnimated:YES completion:nil];
    return NO;
}

On tableview's cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    if(indexPath.section == 0){
        if(indexPath.row == 0){
        cell.textLabel.text = self.contactName;
        NSLog(@"Contact Name %@", self.contactName);
    }
    else{
        cell.textLabel.text = @"";
    }
}
}

When I set to the string, only the firstname property, then the string has the right value, but when I try to concatenate the strings(first +middle+last names) and reload the tableview, I get a null value. What I am I doing wrong and how can I correct it?


Solution

  • What I did wrong was declaring the property contactName as weak

    @property(nonatomic, weak) NSString *contactName;
    

    Also check for null before concatenating strings as Ahmad suggested.