名前、住所、電話番号を持つオブジェクトの配列があります。番号など
名前フィールド、住所フィールドなどで、用語の出現を配列で検索できるようにしたいと考えています。
私はこのようなことを念頭に置いています:
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.searchResults removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchText];
searchResults = [NSMutableArray arrayWithArray:[contactArray filteredArrayUsingPredicate:predicate]];
}
これにより、「コレクションで in/contains 演算子を使用できません」という例外が発生します。
アップデート。最大 3 つのフィールドを検索できるようになりました。4 番目を (任意の順序で) 追加すると、「フォーマット文字列を解析できません ...」という例外が発生します。
Predicate コードは次のようになりました。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.narrative contains[c] %@ OR SELF.category contains[c] %@ OR SELF.date contains[c] OR SELF.name contains[c] %@", searchText, searchText, searchText, searchText];
searchResults = [NSMutableArray arrayWithArray:[allDreams filteredArrayUsingPredicate:predicate]];
述語検索フィールドの制限は 3 つですか? どうすればこれを回避できますか? 再度、感謝します。