Note that there are some explanatory texts on larger screens.

plurals
  1. PONSPredicate against NSArray in iphone
    primarykey
    data
    text
    <p>How can I use <code>NSPredicate</code> to filter the values from array ivar against search query ? I manage to use <code>NSPredicate</code> against string ivar. I have one user defined class named as "User" which is used to create User from AddressBook. Here is my code</p> <pre><code> // User Defined Class named as "User" // In User.h @interface User : NSObject { NSString *firstName; NSString *lastName; NSString *company; UIImage *userImage; NSArray *phoneNumberArray; NSArray *emailArray; NSArray *urlArray; NSArray *addressArray; NSString *notes; NSString *dateOfBirth; } // In SearchViewController.m - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ // contactsArray is NSMutableArray contactsArray = [Search searchContactsWithQuery:searchText]; NSLog(@"contactsArray count =&gt; %d",[contactsArray count]); [contactsTableView reloadData]; } // In Search.m +(NSMutableArray*)searchContactsWithQuery:(NSString*)query { NSLog(@"Query =&gt; %@",query); NSMutableArray* predicates=[[NSMutableArray alloc] init]; // Create all predicates NSPredicate * firstNamePredicate = [NSPredicate predicateWithFormat:@"firstName contains %@",query]; [predicates addObject:firstNamePredicate]; NSPredicate * lastNamePredicate = [NSPredicate predicateWithFormat:@"lastName contains %@",query]; [predicates addObject:lastNamePredicate]; NSPredicate * companyPredicate = [NSPredicate predicateWithFormat:@"company contains %@",query]; [predicates addObject:companyPredicate]; // Don't know how to use on array // === START of ARRAY Predicate ==== NSPredicate *phoneNoPredicate = [NSPredicate predicateWithFormat:@"phoneNumberArray IN %@",query]; [predicates addObject:phoneNoPredicate]; NSPredicate *emailPredicate = [NSPredicate predicateWithFormat:@"emailArray contains %@",query]; [predicates addObject:emailPredicate]; NSPredicate *urlPredicate = [NSPredicate predicateWithFormat:@"urlArray contains %@",query]; [predicates addObject:urlPredicate]; NSPredicate *addressPredicate = [NSPredicate predicateWithFormat:@"addressArray contains %@",query]; // === END of ARRAY Predicate ==== NSPredicate *notesPredicate = [NSPredicate predicateWithFormat:@"notes contains %@",query]; [predicates addObject:notesPredicate]; NSPredicate *dobPredicate = [NSPredicate predicateWithFormat:@"dateOfBirth contains %@",query]; [predicates addObject:dobPredicate]; // Add predicates to array NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicates]; NSArray * filteredArray = [APP_DELEGATE.allUsersArray filteredArrayUsingPredicate:compoundPredicate]; return [NSMutableArray arrayWithArray:filteredArray]; } </code></pre> <p>phoneNumberArray,emailArray,urlArray &amp; addressArray are used because user may have multiple entries for phone no , email, address , url like HOME , WORK, iPhone, Other etc. </p> <p>How can I use predicates on array? Any kind of help is appreciated. Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload