2017-04-17 30 views
0

我有一个包含NSDictionaries的数组。如何使用字典中的键的值从NSMutableArray中搜索NSDictionary对象

[ 
    { 
    "ObjectId": 20, 
    "ObjectName": "Leave Request", 
    "RequestItems": [] 
    "ClassName": "req-auth" 
    }, 
    { 
    "ObjectId": 22, 
    "ObjectName": "Cancel Leave Request", 
    "RequestItems": [] 
    "ClassName": "req-auth" 
    } 

我想这个数组Leave RequestCancel Leave Request对象可以办理入住手续。所以我要通过ObjectName搜索对象。但我不明白如何通过使用这些对象的1值来搜索这些对象。请帮帮我。 感谢

+0

您的问题相关的问题已经在SO回答了多次。 –

回答

0

试试这个

NSString *searchString = @"Cancel"; 
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) { 
     return [evaluatedObject[@"ObjectName"] localizedCaseInsensitiveContainsString:searchString]; 
}]; 
    filteredContentList = [NSMutableArray arrayWithArray:[searchListValue filteredArrayUsingPredicate:predicate]]; 
0

您可以尝试使用NSPredicate。类似这样的:

-(NSArray *)array:(NSArray *)array filteredWithString:(NSString *)string{ 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.ObjectName CONTAINS[cd] %@",string]; 

    return [array filteredArrayUsingPredicate:predicate]; 
} 

只需传入数组以过滤字符串,然后将其称为您要过滤的字符串。

有几件事情:这里cd用于执行比较不敏感的情况下,如果你想它是区分大小写刚刚从字符串中删除的cdObjectName是您要过滤的财产。如果您想要进行精确比较,您可以将CONTAINS更改为LIKE

0

如果你只是想从阵列中的每个字典取objectName,然后做这样:

for dict in arrayOfData{ 
     if let strObjectName : String = dict["ObjectName"] as? String{ 
     print(strObjectName) 
     } 
    } 
+0

您可以创建一个函数,在该函数中为OP传递一个字符串,以便仅在'ObjectName'中获取他想要的值。然后在where子句中使用这个字符串,使它成为'if let strObjectName = dict [“ObjectName”] as? String,strObjectName.contains(“Cancel”)',然后将字典追加到一个新数组中 – Rikh

相关问题