【问题标题】:how to evaluate a NSDictionary in NSPredicate如何在 NSPredicate 中评估 NSDictionary
【发布时间】:2015-09-09 07:43:01
【问题描述】:

我有一个字典有两个数组,字典是

NSDictionary * StudentDetails = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:StudentIDs,StudentNames, nil] forKeys:[NSArray arrayWithObjects:@"ID",@"NAME", nil]];

两个数组是

NSArray * StudentIDs = [NSArray arrayWithObjects:@"07bg1234",@"07ac1234", nil];
NSArray * StudentNames = [NSArray arrayWithObjects:@"aaaaaaa",@"bbbbb", nil];

我尝试使用谓词来检索数组值

NSString * searchStr = @"07bg1234";

    NSPredicate * fileExtPredicate01 = [NSPredicate predicateWithFormat:@"SELF IN %@", StudentIDs];

    if (([fileExtPredicate01 evaluateWithObject: searchStr]== YES)) {
        NSLog(@"Student ID: %@ Exists",searchStr);
    }
    else {
        NSLog(@"Student ID: %@ not Exists",searchStr);
    }

到目前为止,它工作正常....当我尝试将字典值获取为时

NSPredicate * fileExtPredicate02 = [NSPredicate predicateWithFormat:@"SELF IN %@", StudentDetails];

    if (([fileExtPredicate02 evaluateWithObject: searchStr]== YES)) {
        NSLog(@"Student Details: %@ Exists",searchStr);
    }
    else {
        NSLog(@"Student Details: %@ not Exists",searchStr);
    }

我总是得到 o/p 作为学生详细信息 **** 不存在...我如何使用谓词在字典中获取“searchStr”?????提前谢谢你

【问题讨论】:

    标签: ios objective-c nsdictionary nspredicate


    【解决方案1】:

    使用@"SELF IN %@.ID"

    我构建了以下通过单元测试。

    NSArray * StudentIDs = [NSArray arrayWithObjects:@"07bg1234",@"07ac1234", nil];
    NSArray * StudentNames = [NSArray arrayWithObjects:@"aaaaaaa",@"bbbbb", nil];
    NSDictionary * StudentDetails = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:StudentIDs,StudentNames, nil] forKeys:[NSArray arrayWithObjects:@"ID",@"NAME", nil]];
    NSString *searchStr = @"07bg1234";
    
    NSPredicate * fileExtPredicate02 = [NSPredicate predicateWithFormat:@"SELF IN %@.ID", StudentDetails];
    
    XCTAssert([fileExtPredicate02 evaluateWithObject:searchStr]);
    

    更新

    为了同时获得 ID 和 NAME,我使用了。

    NSArray * StudentIDs = [NSArray arrayWithObjects:@"07bg1234",@"07ac1234", nil];
    NSArray * StudentNames = [NSArray arrayWithObjects:@"aaaaaaa",@"bbbbb", nil];
    NSDictionary * StudentDetails = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:StudentIDs,StudentNames, nil] forKeys:[NSArray arrayWithObjects:@"ID",@"NAME", nil]];
    
    NSPredicate * fileExtPredicate02 = [NSPredicate predicateWithFormat:@"SELF IN %@.ID OR SELF IN %@.NAME", StudentDetails, StudentDetails];
    
    XCTAssert([fileExtPredicate02 evaluateWithObject:@"07bg1234"]);
    XCTAssert([fileExtPredicate02 evaluateWithObject:@"aaaaaaa"]);
    

    这对我来说感觉很笨拙,我认为使用 SUBQUERY() 会有更好的答案,但我必须再考虑一下。


    更新 2

    因此,如果您能忍受一些不稳定,这里是SUBQUERY() 版本。谓词是SUBQUERY(%@, $value, SELF IN $value).@count > 0SUBQUERY(%@, $value, SELF IN $value) 返回一个匹配值数组,.@count > 0 如果至少有 1 个匹配则返回 true。与之前的示例不同,我没有通过StudentDetails。我需要通过StudentDetails.allValues

    NSArray * StudentIDs = [NSArray arrayWithObjects:@"07bg1234",@"07ac1234", nil];
    NSArray * StudentNames = [NSArray arrayWithObjects:@"aaaaaaa",@"bbbbb", nil];
    NSDictionary * StudentDetails = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:StudentIDs,StudentNames, nil] forKeys:[NSArray arrayWithObjects:@"ID",@"NAME", nil]];
    
    NSPredicate * fileExtPredicate02 = [NSPredicate predicateWithFormat:@"SUBQUERY(%@, $value, SELF IN $value).@count > 0", StudentDetails.allValues];
    
    XCTAssert([fileExtPredicate02 evaluateWithObject:@"07bg1234"]);
    XCTAssert([fileExtPredicate02 evaluateWithObject:@"aaaaaaa"]);
    

    【讨论】:

    • searchStr 可能在 ID 或 NAME 中。上面的代码建议限制搜索,searchStr 只检查 ID .. 但我必须检查整个字典中的值
    • @Sunil 抱歉,您的问题并不清楚。我会更新我的答案。
    • 对此感到抱歉..但我提到“如何使用谓词在字典中获取'searchStr'”.....因为我可以将searchStr更改为我的希望权利作为 ID 的值或 NAME 的值
    • @Sunil 我使用SUBQUERY 创建了一个不同的答案。希望其中一个有用。
    【解决方案2】:

    对于字典,您需要条件 <Item> IN <Key> 对应于您的示例中的 @"07bg1234" IN @"ID"evaluateWithObject 中的对象是字典。

    NSPredicate *fileExtPredicate02 = [NSPredicate predicateWithFormat:@"%@ IN ID", searchStr];
    if ([fileExtPredicate02 evaluateWithObject: studentDetails] == YES) {
      NSLog(@"Student Details: %@ Exists",searchStr);
    }
    else {
      NSLog(@"Student Details: %@ not Exists", searchStr);
    };
    

    【讨论】:

    • 如果 searchStr 是 StudentIDs 中的值,那没关系。如果 searchStr 是 StudentNames 中的值,它不起作用。在谓词分配中,您没有提到字典?只有键值就够了吗???
    • 当然StudentNames中的项目是关键NAME
    • searchStr 可能在 ID 或 NAME 中。上面的代码建议将 searchStr 限制为 ID ...我有一个想法,它不限制 searchStr 但它需要一个更多谓词。由于字典包含 n 个数组,我必须创建 n 个谓词。 . 有没有其他简单格式的方法
    猜你喜欢
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 2013-08-06
    • 2012-08-15
    • 2023-03-28
    相关资源
    最近更新 更多