iphone - Using NSPredicate with nested arrays ios -
i have array this
nsmutablearray *level1; nsmutablearray *level2; nsmutablearray *level3; self.questionsections=[[nsarray alloc] initwithobjects:@"level 1",@"level 2",@"level 3", nil]; level1=[[nsmutablearray alloc] init]; level2=[[nsmutablearray alloc] init]; level3=[[nsmutablearray alloc] init]; [level1 addobject:[[nsdictionary alloc] initwithobjectsandkeys: @"what opposite of up?",@"name", @"101q.png",@"questionpicture", nil]]; [level1 addobject:[[nsdictionary alloc] initwithobjectsandkeys: @"what opposite of front?",@"name", @"102q.png",@"questionpicture", nil]]; [level2 addobject:[[nsdictionary alloc] initwithobjectsandkeys: @"name common pet?",@"name", @"201q.png",@"questionpicture", nil]]; [level2 addobject:[[nsdictionary alloc] initwithobjectsandkeys: @"name common bird?",@"name", @"202q.png",@"questionpicture", nil]]; [level3 addobject:[[nsdictionary alloc] initwithobjectsandkeys: @"list 3 rooms in house?",@"name", @"301q.png",@"questionpicture", nil]]; self.questiondata=[[nsarray alloc] initwithobjects:level1,level2,level3, nil]; and want search it. trying getting nowhere
nspredicate *resultpredicate=[nspredicate predicatewithformat:@"name contains 'the'"]; nslog(@"%@",resultpredicate); nslog(@"%@",self.questiondata); self.searchresults=[self.questiondata filteredarrayusingpredicate:resultpredicate]; nslog(@"%@",self.searchresults); resultpredicate looks fine "name contains 'the'"
self.questiondata looks fine
but self.searchresults ( )
any ideas great. thanks.
here problem array.
( ( { name = "what opposite of up?"; questionpicture = "101q.png"; }, { name = "what opposite of front?"; questionpicture = "102q.png"; } ), ( { name = "name common pet?"; questionpicture = "201q.png"; }, { name = "name common bird?"; questionpicture = "202q.png"; } ), ( { name = "list 3 rooms in house?"; questionpicture = "301q.png"; } ) ) for working above predicate array must this,
( { name = "what opposite of up?"; questionpicture = "101q.png"; }, { name = "what opposite of front?"; questionpicture = "102q.png"; }, { name = "name common pet?"; questionpicture = "201q.png"; }, { name = "name common bird?"; questionpicture = "202q.png"; }, { name = "list 3 rooms in house?"; questionpicture = "301q.png"; } ) edit:- try this,
nsarray *questiondata=[[nsarray alloc] initwithobjects:level1,level2,level3, nil]; nspredicate *resultpredicate=[nspredicate predicatewithformat:@"name contains 'the'"]; nsmutablearray *resultarray= [[nsmutablearray alloc]init]; for(int i=0;i<[questiondata count];i++){ nsarray* searchresults=[[questiondata objectatindex:i] filteredarrayusingpredicate:resultpredicate]; if([searchresults count]>0) [resultarray addobject:searchresults]; } nslog(@"%@",resultarray); o/p:- ( ( { name = "what opposite of up?"; questionpicture = "101q.png"; }, { name = "what opposite of front?"; questionpicture = "102q.png"; } ) )
Comments
Post a Comment