0

假设我有一个具有以下数据的对象(以 JSON 表示)。我很想知道是否有一种方法可以编写子查询或使用不带块的 NSPredicate 直接提取那些仅具有“W”状态的测试的主题。

即仅从 NSPredicates 获取以下内容。

NSArray *list = [self.event.subjects filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(subject *subject, NSDictionary *bindings) {
    for (test *test in subject.tests) {
        if ([test.status.lowercaseString isEqualToString:@"w"]) {
            return YES;
        }
    }
    return NO;
}]];



{
"id": 860142,
"subjectList": [
    {
        "id": 12206971,
        "testList": [
            {
                "id": 76904260,
                "status": "W"
            },
            {
                "id": 76904291,
                "status": "L"
            }
        ]
    },{
        "id": 12206971,
        "testList": [
            {
                "id": 76904260,
                "status": "L"
            },
            {
                "id": 76904291,
                "status": "L"
            }
        ]
    }
]

}

4

1 回答 1

0

找到了这个解决方案......我认为这只是一个被 SUBQUERY 吓倒的情况,因为解决方案原来是如此简单。

 [self.event.subjects filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SUBQUERY(tests, $x, $x.status.lowercaseString == 'w').@count > 0"]];
于 2013-08-15T01:40:33.170 回答