2011-09-27 6 views
2

異なる基準に基づいてnsdictionaryからデータを検索したいと思います.4つのキーがあり、ユーザーは任意の数のキーに基づいて検索できます。例:4,3 etc..Ex:キーを含む辞書(名字、姓私は空の文字列を提供する場合はnsdictionaryをフィルタリングする方法は?

を入力してください。

これはどのように達成できますか?またはユーザーが提供しているフィールドに基づいて複数のnspredicateを作成する必要がありますか?

ありがとうございます!

答えて

2

あなたがプログラム的述語を構築することができます:

NSArray *keysToSearch = [NSArray arrayWithObjects:@"FirstName", @"LastName", @"Address", nil]; 
NSString *searchString = @"Bob"; 

NSMutableArray *subPredicates = [NSMutableArray array]; 
for (NSString *key in keysToSearch) { 
    NSPredicate *p = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", key, searchString]; 
    [subPredicates addObject:]; 
} 

NSPredicate *filter = [NSCompoundPredicate andPredicateWithSubPredicates:subPredicates]; 

を次に、あなたの辞書をフィルタリングするfilterを使用しています。

関連する問題