【问题标题】:Searching dictionary in array using predicate使用谓词在数组中搜索字典
【发布时间】:2014-09-10 07:35:12
【问题描述】:

我有一个字典数组,我想为键“guid”找到具有所需值的字典。 无法使用此代码获取它:

NSPredicate *filter = [NSPredicate predicateWithFormat:@"guid = %@", key];
NSArray *filteredContacts = [selectedValue filteredArrayUsingPredicate:filter];

【问题讨论】:

  • 请尝试记录数组以获得更多理解

标签: ios iphone arrays nsdictionary predicate


【解决方案1】:

试试这个。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Name CONTAINS[cd] %@",searchBar.text];

filterdArray = [[hospitalArray filteredArrayUsingPredicate:predicate] mutableCopy];

其中Name是数组下字典中包含的键。

【讨论】:

    【解决方案2】:

    我想你忘记了“=”号。

     NSPredicate *filter = [NSPredicate predicateWithFormat:@"guid == %@", key];
     NSArray *filteredContacts = [selectedValue filteredArrayUsingPredicate:filter];
    

    【讨论】:

    • filteredArrayUsingPredicate 后我没有。
    • 我有字典数组,并为它的值搜索字典
    • 请尝试 NSLog(%@, selectedValue);并复制您帖子中的内容
    【解决方案3】:

    运算符条件已忘记;)

    你需要像这样添加一个双“=”:

    NSPredicate *filter = [NSPredicate predicateWithFormat:@"guid == %@", key];
    NSArray *filteredContacts = [selectedValue filteredArrayUsingPredicate:filter];
    

    希望对你有帮助!

    【讨论】:

    • 糟糕,答案与@PratikMistry 相同,抱歉!
    • 对,我回答他,请看
    【解决方案4】:
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(guid CONTAINS[c] %@)",key];
    NSArray *array = [[NSArray arrayWithArray:sampleArray] filteredArrayUsingPredicate: predicate];
    

    在上面的代码中,“fromDate”是字典的键。

    我希望这对你有用。

    【讨论】:

      【解决方案5】:

      试试这个,

      NSArray *filtered = [array_with_dict filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(guid == %@)", @"desired value"]];
      

      【讨论】:

        【解决方案6】:

        您可以尝试将 NSPredicate 启动为:

        NSPredicate *filter = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"guid = '%@'", key]];
        

        希望对你有帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-23
          • 1970-01-01
          • 1970-01-01
          • 2017-08-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多