ios
ios

Reputation: 975

App crashes when i start typing in uiseachbar in uitableview

I am making a project in which i am fetching addressbook in my tableview with the help of NSObject class.

Now , I allocated a UISearchbar in it , but when i start typing , the app crashes with the following error :---- 'Can't use in/contains operator with collection (not a collection)'.

my code

-(void)loadContacts { 
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

   if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
       ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
          ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
          //NSLog(@"%@", addressBook);
      });
   } else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
       CFErrorRef *error = NULL;
       ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
       CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
       CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

       for(int i = 0; i < numberOfPeople; i++) {
           ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
           NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
           NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));

           ContactData *dataObj = [[ContactData alloc]init];
           NSString *Name = @" ";

           if ([firstName length]>0) {
               if ([lastName length]>0) {
                   Name = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
               } else {
                   Name = firstName;
               }
           } else {
               Name = @" ";
           }
           ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
           [[UIDevice currentDevice] name];
           //NSLog(@"\n%@\n", [[UIDevice currentDevice] name]);

           NSString *phoneNumber = @" ";
           for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
              phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
           }

           ABMultiValueRef Emails = ABRecordCopyValue(person, kABPersonEmailProperty);
           [[UIDevice currentDevice] name];
           NSString *Email; //= @" ";
           for (CFIndex i = 0; i < ABMultiValueGetCount(Emails); i++) {
               Email = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(Emails, i);

           }
           ABMultiValueRef Addresses = ABRecordCopyValue(person, kABPersonAddressProperty);

           NSString *address = @" ";
           NSMutableDictionary* addressDict = [[NSMutableDictionary alloc]init];

           for (CFIndex i = 0; i < ABMultiValueGetCount(Addresses); i++) {
               addressDict = (__bridge_transfer NSMutableDictionary *) ABMultiValueCopyValueAtIndex(Addresses, i);

               //NSLog(@"address = %@", addressDict);

               NSString * street = [addressDict objectForKey:@"Street"];
               NSString * city = [addressDict objectForKey:@"City"];
               NSString * state = [addressDict objectForKey:@"State"];
               NSString *country = [addressDict objectForKey:@"Country"];


              if (country.length>0 || state.length>0 || city.length>0 || street.length>0) {

                   if (country.length>0 && state.length>0) {
                       address = [NSString stringWithFormat:@"%@, %@", state,country];
                   } else if(country.length>0 && city.length>0){
                       address = [NSString stringWithFormat:@"%@, %@", city,country];
                   } else if (state.length>0 && street.length>0){
                       address = [NSString stringWithFormat:@"%@, %@", street,country];
                   } else if (state.length>0 && city.length>0){
                       address = [NSString stringWithFormat:@"%@, %@", city,state];
                   } else if (state.length>0 && street.length>0) {
                       address = [NSString stringWithFormat:@"%@, %@", street,state];
                   } else if (city.length>0 && street.length>0) {
                       address = [NSString stringWithFormat:@"%@, %@", street,city];
                   } else if (country.length>0) {
                       address = country;
                   } else if (state.length>0) {
                       address = state;
                   } else if (city.length>0) {
                       address = city;
                   }
               } else {
                   address = @" ";
               }
           }

           //NSLog(@"address = %@", address);

           NSData *imgData = (__bridge_transfer NSData *) ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);

           dataObj.name = Name;
           dataObj.email = Email;
           dataObj.phone = phoneNumber;
           dataObj.imageData = imgData;
           dataObj.address = address;

           [contactArr addObject:dataObj];
       }
       NSLog(@"contacts loaded");
       //NSLog(@"count = %lu", (unsigned long)contactArr.count);
  } else {
       //Go to settings and allow access to use contacts[GlobalFunction removeWaitView];
  }

code for search bar is

     - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
      {

        NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
        searchResults = [contactArr filteredArrayUsingPredicate:resultPredicate];
        }


        -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
        {
         [self filterContentForSearchText:searchString scope:   [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
           return YES;

}

Upvotes: 0

Views: 78

Answers (2)

Babul Prabhakar
Babul Prabhakar

Reputation: 2403

It seems that the Predicate is incorrect Try :

   NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.fullName contains[c] %@", searchText];

Upvotes: 1

ios
ios

Reputation: 975

Here is cellForRowAtIndexPath

  -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
static NSString *cellIdentifier=@"CellID";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell==nil)
  {

   cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
   cell.backgroundColor=[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:239.0/255.0 alpha:1.0];

}


if (tableView == self.searchDisplayController.searchResultsTableView) {
    personContact = [searchResults objectAtIndex:indexPath.row];

}
else {
    personContact = [contactsArr objectAtIndex:indexPath.row];

}

  cell.textLabel.text = personContact.fullName;

return cell;

}

Upvotes: 0

Related Questions