ios NSPredicate在数组对象中搜索

时间:2015-08-28 11:55:53

标签: ios search nspredicate

如何搜索我有这种类型的数组 这是数组的一个对象

@interface Recipe : NSObject

@property (nonatomic, strong) NSString* recipeName;
@property (nonatomic, strong) NSString* recipeId;
@property (nonatomic, retain) NSString* rcpDescription;
@property (nonatomic, retain) NSString* servingSize;
@property (nonatomic, retain) NSString* totalCalories;

这里我从tableview

中的数组数据设置recipename
Recipe* aRecipe = [recipeAry objectAtIndex:indexPath.row];
    cell.lbl_name = aRecipe.recipeName
问题是当我使用NSPredicate时无法搜索到这里是我的代码请给我解决方案

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.recipeName= %@", searchText];
       // SELF.shipBuilder LIKE[cd] %@

        self.searchData = [recipeAry filteredArrayUsingPredicate:predicate];
        NSLog(@"%@",self.searchData);

2 个答案:

答案 0 :(得分:0)

尝试使用%K




  NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@“%K包含[cd]”, recipeName,searchText];
  



答案 1 :(得分:0)

使用以下代码过滤来自 Recipe 对象的给定数组的结果

NSArray *recipeAry; //your data array

//create a search predicate to filter the array
NSPredicate *searchPredicate = [NSPredicate predicateWithBlock:^BOOL(Recipe *evaluatedObject, NSDictionary *bindings) {

    return [evaluatedObject.recipeName hasPrefix:searchText];
}];

self.searchData = [recipeAry filteredArrayUsingPredicate:searchPredicate]; //this is the filtered array you can use for the displaying UI with search results.