views:

437

answers:

1

Hey everyone,

Apple provides the following sample code in their QuickContacts project for how to search the address book for a particular user.

-(void)showPersonViewController
{
 // Fetch the address book 
 ABAddressBookRef addressBook = ABAddressBookCreate();

 // Search for the person named "Appleseed" in the address book
 CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook, CFSTR("Appleseed"));

 // Display "Appleseed" information if found in the address book 
 if ((people != nil) && (CFArrayGetCount(people) > 0))
 {
  ABRecordRef person = CFArrayGetValueAtIndex(people, 0);
  ABPersonViewController *picker = [[[ABPersonViewController alloc] init] autorelease];
  picker.personViewDelegate = self;
  picker.displayedPerson = person;
  // Allow users to edit the person’s information
  picker.allowsEditing = YES;

  [self.navigationController pushViewController:picker animated:YES];
 }
 else 
 {
  // Show an alert if "Appleseed" is not in Contacts
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
              message:@"Could not find Appleseed in the Contacts application" 
                delegate:nil 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:nil];
  [alert show];
  [alert release];
 }
 CFRelease(addressBook);
 CFRelease(people);
}

The line that I'm having trouble with is:

// Search for the person named "Appleseed" in the address book
CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook, CFSTR("Appleseed"));

This will search the address book for the person named "Appleseed", but I want to search the address book based on a user that is stored in a variable. For example, I'm trying:

Customer *customer = [customerArray objectAtIndex:indexPath.row];
cell.textLabel.text = customer.name;

CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook, CFSTR(customer.name));

The "customer.name" isn't resolving to the value that is stored. I used NSLog to output the value of customer.name and it holds the expected value.

How can I resolve this variable to a string so it will search the address book properly?

Thanks!

+1  A: 

Is customer.name an NSString?

CFSTR only accepts literal C strings.
To pass an NSString, cast it to CFStringRef:

CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook,
                                        (CFStringRef)customer.name);
DyingCactus
customer is an array, should I be able to cast "customer.name" as an array similar to how you've shown it as an NSString?
scottystang
According to your code sample, customerArray (not customer) is an array of Customer objects, customer is an object of type Customer, and name is a property in the Customer object. "name" seems to be of type NSString since you are assigning it to cell.textLabel.text. In any case, you need to pass a CFStringRef to that address book method. A CFStringRef points to one string--not an array.
DyingCactus
Thanks, that helps clear things up. However, when I try and cast customer.name as CFStringRef, I get "EXC_BAD_ACCESS" in the console when it hits that line. Any ideas?
scottystang
Show how the name property is defined in the Customer.h file.
DyingCactus
I assume you're doing this in cellForRowAtIndexPath. Make sure customer is not nil for any row or that customer.name is not nil for any row.
DyingCactus