2012-03-28 13 views
1

Wie kann ich NSPredicate verwenden, um die Werte von Array-ivar gegen Suchanfrage zu filtern? Ich schaffe es, NSPredicate gegen String Ivar zu verwenden. Ich habe eine benutzerdefinierte Klasse mit dem Namen "Benutzer", die verwendet wird, um Benutzer aus Adressbuch zu erstellen. Hier ist mein CodeNSPredate gegen NSArray in iphone

// 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 => %d",[contactsArray count]); 
    [contactsTableView reloadData]; 
} 

// In Search.m 

+(NSMutableArray*)searchContactsWithQuery:(NSString*)query { 

    NSLog(@"Query => %@",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]; 

} 

phoneNumberArray, emailArray, urlArray & addressArray verwendet werden, da Benutzer nicht mehrere Einträge für Telefon haben, E-Mail, Adresse, URL wie zu Hause, Arbeit, iPhone, Andere usw.

Wie Kann ich Prädikate für Arrays verwenden? Jede Art von Hilfe wird geschätzt. Danke

+1

'filteredArrayUsingPredicate:' ist der richtige Weg zu gehen. Funktioniert der obige Code nicht? Sind Sie sicher, dass im Array ein Wert vorhanden ist, der dem Prädikat entspricht? – Caleb

+0

Es gibt Werte im Array, die mit Prädikat übereinstimmen – iOSAppDev

+0

Versuchen Sie, Ihr Prädikat zu vereinfachen. Versuchen Sie nicht, das Verbundprädikat zu verwenden, sondern das Array mit einem Ihrer einfachen Prädikate zu filtern. In der Tat könnten Sie versuchen, sie jeweils anzuwenden, um 9 separate Arrays zu erhalten - das könnte Ihnen einen Hinweis geben, wo das Problem liegt. – Caleb

Antwort

0

Sie machen den richtigen Weg, aber Sie müssen Ihre Prädikate mit String-Werten erstellen, anstatt die Objekte direkt zu übergeben.

Sie benötigen anstelle von% @% k verwenden, wie

[NSPredicate predicateWithFormat:@"firstName contains %k",query];

Bitte lassen Sie mich wissen, ob das hilft.