2015-08-28 114 views
0

如何搜索時,我有這種類型的陣列 這裏的是陣列在數組對象IOS 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); 
+0

什麼'searchText'的價值?你試圖找到什麼價值?你在尋找'CONTAINS [c]'而不是? – Larme

+0

我想搜索recipeName,但是當我nslog我的數組顯示 like this and search data is null –

+0

SPredicate * predicate = [NSPredicate predicateWithFormat:@「SELF.recipeName CONTAINS [cd]%@」,searchText]; –

回答

0

嘗試使用%K

NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"%K contains[cd]", recipeName, searchText]; 
0

使用下面的代碼從方給定的數組來篩選結果對象

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.