ios - 如何评估 NSPredicate 中的 NSDictionary

标签 ios objective-c nsdictionary nspredicate

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

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 作为 Student Details **** not Exists... 我怎样才能使用谓词在字典中得到 'searchStr' 是否存在????提前致谢

最佳答案

使用 @"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) 返回匹配值数组,如果至少有 1 个匹配项,则 .@count > 0 返回 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"]);

关于ios - 如何评估 NSPredicate 中的 NSDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32473718/

相关文章:

ios - 来自 HTML 的 NSAttributedString

c# - 如何在 xamarin ios 中自定义选项卡位置

ios - 从 URL 读取字节,并将二进制转换为普通十进制。对象C

ios - 删除/插入文本后,UITextView向上滚动

iphone - 定义将由类方法使用的私有(private)方法 - Objective C

iOS 设备与蓝牙设备的通信

objective-c - 在 Cocoa 和 NSTask 中安全地处理密码

ios - NSArray 不按数字顺序排序

ios - 无法在 Swift 中将类型 '__NSSingleObjectArrayI' 的值转换为 'NSDictionary'

ios - NSdictionary 作为数据类型