简体   繁体   English

使用谓词对数组的字典进行排序

[英]Sort Dictionary of Array Using Predicate

I have a json dictionary 我有一个json字典

[
  {
    "allquestions": [
      {
        "question": "First question in 0th index"
      },
      {
        "question": "Second  question in 0th index"
      }
    ]
  },
  {
    "allquestions": [
      {
        "question": "First  question in 1st index"
      }
    ]
  }
]

I need to get all values of key allquestions in single array using predicate. 我需要使用谓词来在单个阵列关键allquestions的所有值。

I have tried so far is 我到目前为止已经尝试过

NSPredicate *combinePagePredicate = [NSPredicate predicateWithFormat:@"allquestions == %@",@"someValue"];

What will be the value of someValue to solve my problem? someValue解决我的问题的价值是什么?

Not entirely sure what your trying to achieve. 不确定您要达到的目标。 If you want a list of all of the questions in that dictionary then iterate through each dictionary separately and then the same for the questions. 如果要获取该词典中所有问题的列表,请分别遍历每个词典,然后对这些问题进行相同的遍历。 Add the questions to an array and then filter that array. 将问题添加到数组,然后过滤该数组。 Not tried this code out but it's a start. 没有尝试过此代码,但这是一个开始。

NSMutableArray *questions = [[NSMutableArray alloc] init]; 
for (id i in dictionaryArray)
{
    NSArray *arr = i[@"allquestions"];
    for (id x in arr) 
    {
       NSString *currentQuestion = [x objectForKey:@"question"];
       [questions addObject:currentQuestion]; 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM