garaster 2013-07-19 06:47 采纳率: 0%
浏览 2355

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只有()

谢谢。

  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-01-06 14:28
    关注

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


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

    [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?"的字典。

    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程