ios嵌套数组NSpredicate

数组如下:

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];

现在要搜索,但是我做的都徒劳无功:

NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"name CONTAINS 'the'"];

NSLog(@"%@",resultPredicate);
NSLog(@"%@",self.questionData);

self.searchResults=[self.questionData  filteredArrayUsingPredicate:resultPredicate];

NSLog(@"%@",self.searchResults);

resultPredicate和self.questionData都正确

但是self.searchResults只有()

谢谢。

这里的问题可能是在创建字典时给定的键值不匹配。


例如,在下面这行代码中:

[level1 addObject:[[NSDictionary alloc] initWithObjectsAndKeys:
                   @"What is the opposite of up?",@"name",
                   @"101q.png",@"questionpicture",
                   nil]];

字典中的键是"name",值是"What is the opposite of up?"。而在谓词中,您正在搜索"name"键的值,即"the"。这就是为什么"self.searchResults"是空的原因。


为了解决这个问题,您需要更改谓词,使其与字典中的键值匹配。例如,您可以更改谓词如下:

NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"name CONTAINS 'up'"];

这样,您就可以在"self.searchResults"中找到包含"What is the opposite of up?"的字典。