使用NSPredicate在数组中搜索

时间:2016-01-04 14:12:17

标签: ios nspredicate

我有一个NSArray,项目是ItemModel(id,type,status,UserModel,...),UserModel包括:name,type。

我想使用NSPredicate搜索数组中的项,其中键是UserModel的名称。我该怎么做?

1 个答案:

答案 0 :(得分:0)

试试这个

arraySearch = [ItemModel1, ItemModel2, ItemModel3, ItemModel4];
NSString *textToSearch = @"Anything you want to search";


NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like %@",textToSearch]; 
// name is the attribute of the model, for other attributes use other attribute names there.
//Basically this code means creating a query where the array will be searched which contains the textToSearch characters.  

NSArray *result = [arraySearch filteredArrayUsingPredicate:predicate];
//your result will be returned in the array.

要获得准确的结果,请尝试此

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@",textToSearch];