2

I am trying to do contacts app on Xcode, The contacts list displays but when I click on any name it throws error EXC_BAD_ACCESS, I have checked with NSLog and find that my array gets error while populating the tableview it self, this is my code:

//Array initialized as

@property (strong,nonatomic) NSMutableArray *filteredData,*contactAdd;

//Array populated on this function

-(void)reloadAddressBook
{
    self.contactAdd = [[NSMutableArray alloc]init];    
    ABAddressBookRef addressBook = ABAddressBookCreate();//ABAddressBookCreateWithOptions(NULL,NULL);
    if(ABAddressBookHasUnsavedChanges(addressBook))
    {

        ABAddressBookSave(addressBook,NULL);
    }

    NSMutableArray *allPeople = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
    int nPeople = ABAddressBookGetPersonCount(addressBook);

    for(int i=0; i < nPeople; i++ )
    {
        ABRecordRef person = (__bridge ABRecordRef)([allPeople objectAtIndex:i]);

        [self.contactAdd addObject:(__bridge id)(person)];

        NSLog(@"@details %@",contactAdd);
        CFRelease(person);
    }
    CFRelease(addressBook);
}

//Error in this function

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
                   static NSString *cellIdentifier;


            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];


            if (cell == nil)
                {
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
               }

            if(isFiltered)
            {
               ABRecordRef person = (__bridge ABRecordRef)[self.filteredData objectAtIndex:indexPath.row];
                NSString *tweet=[[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
                [cell.textLabel setText:tweet];
                CFRelease(person);

            }
            else
            {
                ABRecordRef person = (__bridge ABRecordRef)([self.contactAdd objectAtIndex:indexPath.row]);
                NSLog(@"%@",person);
                NSString *tweet=[[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
                [cell.textLabel setText:tweet];
                CFRelease(person);
                NSLog(@"%@",indexPath);
// I have 4 contacts currently and error occurs here on 4th time and when I continue error occurs on 2nd time
                NSLog(@"@details %@",contactAdd); 
}
return cell;
}

//Getting error here in runtime

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ABPersonViewController *currentPersonView=[[ABPersonViewController alloc]init];

    ABRecordRef person;
    if(isFiltered)
        person=(__bridge ABRecordRef)([self.filteredData objectAtIndex:indexPath.row]);
    else
        person=(__bridge ABRecordRef)([self.contactAdd objectAtIndex:indexPath.row]);//error here

    currentPersonView.displayedPerson=person;
    currentPersonView.allowsEditing=YES;
    currentPersonView.allowsActions=YES;
    [self.navigationController pushViewController:currentPersonView animated:YES];

}

//NSLog

2013-08-01 13:11:12.715 Contacts[7722:207] 2 indexes [0, 0]

Current language: auto; currently objective-c 2013-08-01 13:11:26.614 Contacts[7722:207] @details ( "CPRecord: 0xba1b340 ABPerson", "CPRecord: 0xba1c870 ABPerson", "CPRecord: 0xba1c160 ABPerson", "CPRecord: 0xba1c990 ABPerson" )

2013-08-01 13:11:26.616 Contacts[7722:207]

2013-08-01 13:11:26.617 Contacts[7722:207] 2 indexes [0, 1]

2013-08-01 13:11:27.566 Contacts[7722:207] @details ( "CPRecord: 0xba1b340 ABPerson", "CPRecord: 0xba1c870 ABPerson", "CPRecord: 0xba1c160 ABPerson", "CPRecord: 0xba1c990 ABPerson" ) 2013-08-01 13:11:27.567 Contacts[7722:207] 2013-08-01 13:11:27.568 Contacts[7722:207] 2 indexes [0, 2] (gdb)

4 Answers 4

0

First put a value for your cellIdentifier:

static NSString *cellIdentifier = @"CellIdentifier";

0

Don't leave static NSString *cellIdentifier; without a value, example:

static NSString *cellIdentifier = @"SimpleTableCell"

Once you have added the cellIdentifier into your .m file, ensure you have added one for your tableView cell in your Storyboard

See this image: http://www.appcoda.com/wp-content/uploads/2012/04/SimpleTable-Cell-Identifier.jpg

(Sorry, I can't embed the image until I've progressed on stackoverflow further!)

4
  • I have only a view, table view and search bar in my storyboard, havent added tableview cell
    – Francis F
    Aug 1, 2013 at 8:58
  • Your table view will require cells, either static or custom? As you are populating the table with information from the method: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, it returns a cell. Add a cell to the tableview in your storyboard Aug 1, 2013 at 9:00
  • Re-reading your question, when you start the app - does it load a list of contacts? And once they are loaded into the tableView, is it then when you select one of the contacts you receive the error? If it is, then we would need to see your didSelectRowAtIndex method Aug 1, 2013 at 9:35
  • Yes it receives error when I click on one of the contacts, I have added my didSelectRowAtIndex in my question.
    – Francis F
    Aug 1, 2013 at 9:41
0

You need to look at how your contactAdd array is being managed. Your contactAdd array is being created with the intent of populating with 100 objects, but you only have four contacts.

self.contactAdd = [[NSMutableArray alloc]initWithCapacity:100];

This should be populated only with the number of contacts that actually exists. But you also are adding objects to the array irrespective of those initial 100 slots.

[self.contactAdd addObject:(__bridge id)(person)];

Right now, when you go to select a person, there might not be an object to actually work with, as you noted with your own comment.

person=(__bridge ABRecordRef)([self.contactAdd objectAtIndex:indexPath.row]);//error here

Given your code what ought to happen is just simply create the array and then let the population happen naturally later:

self.contactAdd = [[NSMutableArray alloc]init];

Also, during population, there are a few things going on that don't make much sense. First you add the person object, but then you move the person object?

[self.contactAdd addObject:(__bridge id)(person)];
[self.contactAdd replaceObjectAtIndex:i withObject:(__bridge id)(person)];

I'm guessing this has to do with the original 100 slots, but removing that magic number will remove this step as well.

Also, you are releasing an object that you did not retain:

ABRecordRef person = (__bridge ABRecordRef)([allPeople objectAtIndex:i]);
//. . . 
CFRelease(person);

I think you need to review the documentation on working with table views to better understand what is being accomplished here. But, one thing to consider is just doing this...

self.contactAdd = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

...since you are using the objects as given.

The other first two answers are also correct that your cellIdentifier needs to be populated with a string at the time of creation.

2
  • I added initwithcapacity just to check for the issue, initially it was jst init, i forgot to comment out replaceobject and CF release here, it was done while I was debugging for the issue.
    – Francis F
    Aug 1, 2013 at 12:29
  • I think by adding those two things while debugging are doing more harm than good. Aug 1, 2013 at 15:01
0

I was able to solve this issue using CFArray, here is the code incase anyone else needs it. (iphone simulator 5.0)

-(void)reloadAddressBook
{

    if(addressBook)
        CFBridgingRelease(addressBook);
    addressBook = ABAddressBookCreate();
    person=ABAddressBookCopyDefaultSource(addressBook);
    contactAdd=ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(self.addressBook,self.person,0);
    }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellIdentifier;

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];


        if (cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
           }

        if(isFiltered)
        {
           self.person = CFArrayGetValueAtIndex(self.filteredData,indexPath.row);
            NSString *tweet=[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyCompositeName(self.person)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

            [cell.textLabel setText:tweet];


        }
        else
        {
            self.person = CFArrayGetValueAtIndex(self.contactAdd, indexPath.row);
            NSString *tweet=[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyCompositeName(self.person)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
             [cell.textLabel setText:tweet];           
        }


        return cell;
    }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(isFiltered)
       return CFArrayGetCount(self.filteredData);
    else
    {
        if(CFArrayGetCount(self.contactAdd)==0)
        {
            UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Message"
                                                              message:@"Empty Contacts"
                                                             delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles:nil];
            [message show];
        }

        return CFArrayGetCount(self.contactAdd);
    }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.