ios - Multiple NSPredicate -
i'm trying prepare multiple search in coredata entity recipes. there parameters prepare fetch.
recipes attributes:
@property (nonatomic, retain) nsnumber * difficulty; @property (nonatomic, retain) nsstring * name; @property (nonatomic, retain) nsnumber * code; //like identifier @property (nonatomic, retain) nsnumber * preptime; ingredients separate entity list of ingredients.
join entity contains ingredientcode, recipecode, count.
getarrayofjoindatawithingredients fetches join entity , returns nsarray of codes of recipes contains of input ingredient.
here code:
- (ibaction)callrecipefetch:(id)sender { nsstring *predicatestring = @""; nsarray *codes = [[nsarray alloc] init]; codes = nil; if ([paramcontroller.ingredientsforsearcharray count] > 0) { nsmutablearray *ingredientsarray = [[nsmutablearray alloc] init]; (ingredients *ingredient in paramcontroller.ingredientsforsearcharray) { [ingredientsarray addobject:ingredient.code]; } maintabcontroller *maintabcontroller = [[maintabcontroller alloc] init]; codes = [maintabcontroller getarrayofjoindatawithingredients:ingredientsarray]; nsstring *ingrset = [nsstring stringwithformat:@"(code in %@)", codes]; predicatestring = [predicatestring stringbyappendingstring:ingrset]; } nsstring *diff; if ([predicatestring isequaltostring:@""]) { diff = [nsstring stringwithformat:@"(difficulty <= %d)", paramcontroller.diff.selectedsegmentindex + 1]; } else diff = [nsstring stringwithformat:@" , (difficulty <= %d)", paramcontroller.diff.selectedsegmentindex + 1]; predicatestring = [predicatestring stringbyappendingstring:diff]; nsstring *timestring = [nsstring stringwithformat:@" , (%d =< preptime) , (preptime <= %d)", paramcontroller.rangeslider.leftvalue, paramcontroller.rangeslider.rightvalue]; predicatestring = [predicatestring stringbyappendingstring:timestring]; if (paramcontroller.categorycode) { nsstring *categorystring = [nsstring stringwithformat:@" , (incategory = %@)", paramcontroller.categorycode]; predicatestring = [predicatestring stringbyappendingstring:categorystring]; } nspredicate *predicate = [nspredicate predicatewithformat: predicatestring]; [resultcontroller findrecipeswithpredicate:predicate]; } the full predicatestring @"(code in (\n 1,\n 3\n)) , (difficulty <= 5) , (0 =< preptime) , (preptime <= 28800) , (incategory = 12)"
now have error in predicate part (code in %@) when prepare nspredicate code:
nspredicate *predicate = [nspredicate predicatewithformat: predicatestring]; error:
terminating app due uncaught exception 'nsinvalidargumentexception', reason: 'unable parse format string "(code in ( 1, 3 )) , (difficulty <= 5) , (0 =< preptime) , (preptime <= 28800) , (incategory = 12)"' how correctly make predicate in operator. advices.
use nscompoundpredicate multiple predicates, can refer nscompoundpredicate class reference
something this:
nspredicate * andpredicate = [nscompoundpredicate andpredicatewithsubpredicates:[nsarray arraywithobjects:predicate1,predicate2,predicate3,nil]];
Comments
Post a Comment