[英]Using NSPredicate with nested arrays ios
I have an array like 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 is the opposite of up?",@"name",
@"101q.png",@"questionpicture",
nil]];
[level1 addObject:[[NSDictionary alloc] initWithObjectsAndKeys:
@"What is the opposite of front?",@"name",
@"102q.png",@"questionpicture",
nil]];
[level2 addObject:[[NSDictionary alloc] initWithObjectsAndKeys:
@"Name a common pet?",@"name",
@"201q.png",@"questionpicture",
nil]];
[level2 addObject:[[NSDictionary alloc] initWithObjectsAndKeys:
@"Name a common bird?",@"name",
@"202q.png",@"questionpicture",
nil]];
[level3 addObject:[[NSDictionary alloc] initWithObjectsAndKeys:
@"List 3 rooms in your house?",@"name",
@"301q.png",@"questionpicture",
nil]];
self.questionData=[[NSArray alloc] initWithObjects:level1,level2,level3, nil];
And I want to search it. 我想搜索它。 I am trying this but 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'" resultPredicate
看起来很好“名称包含'the'”
self.questionData
looks fine self.questionData
看起来不错
But self.searchResults
is just ( ) 但是self.searchResults
只是()
Any ideas to help would be great. 任何有帮助的想法都会很棒。 Thanks. 谢谢。
I used Subquery to solve this 我用子查询来解决这个问题
NSArray *questionData=[[NSArray alloc] initWithObjects:level1,level2,level3, nil];
NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"SUBQUERY(name, $content, $content CONTAINS %@).@count > 0", @"the"];
NSArray *searchResults=[questionData filteredArrayUsingPredicate:resultPredicate];
NSLog(@"%@",searchResults);
Result:: 结果::
(
(
{
name = "What is the opposite of up?";
questionpicture = "101q.png";
},
{
name = "What is the opposite of front?";
questionpicture = "102q.png";
}
)
) )
here problem with array. 这是数组的问题。
(
(
{
name = "What is the opposite of up?";
questionpicture = "101q.png";
},
{
name = "What is the opposite of front?";
questionpicture = "102q.png";
}
),
(
{
name = "Name a common pet?";
questionpicture = "201q.png";
},
{
name = "Name a common bird?";
questionpicture = "202q.png";
}
),
(
{
name = "List 3 rooms in your house?";
questionpicture = "301q.png";
}
)
)
for working your above predicate then array must be like this, 为了工作上述谓词,则数组必须是这样的,
(
{
name = "What is the opposite of up?";
questionpicture = "101q.png";
},
{
name = "What is the opposite of front?";
questionpicture = "102q.png";
},
{
name = "Name a common pet?";
questionpicture = "201q.png";
},
{
name = "Name a common bird?";
questionpicture = "202q.png";
},
{
name = "List 3 rooms in your house?";
questionpicture = "301q.png";
}
)
EDIT:- try like 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 is the opposite of up?";
questionpicture = "101q.png";
},
{
name = "What is the opposite of front?";
questionpicture = "102q.png";
}
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.