本文介绍了NSPredicate在一对多关系上引用多个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是SQLite永久存储。我有一个 NSManagedObject Den 与一对多关系 Bear Bear 有多个栏位:

I'm using a SQLite persistent store. I have a NSManagedObject class Den with a to-many relationship Bear. Bear has several fields:

Bear:
    breed
    color
    age
    ...

请求我的 Den 对象,我可以筛选具有相关 Bear 具有某个字段值的对象:

When I am building fetch requests for my Den objects, I can filter to objects that have a related Bear with a certain field value:

NSPredicate *hasGrizzlyPred = [NSPredicate predicateWithFormat:@"ANY Bear.breed == 'grizzly'"];

或者我也可以很容易地搜索 c>有一个棕色的熊:

Or I can just as easily search for a Den that has a brown bear:

NSPredicate *hasBrownBearPred = [NSPredicate predicateWithFormat:@"ANY Bear.color == 'brown'"];

但是有任何方法可以搜索 c>有一个熊是棕色和灰熊的?以下是合法的,但不正确,我认为:

But is there any way to search for a Den that has a bear that is both brown and a grizzly? The following is legal, but incorrect, I think:

// Not quite right: search for a den with a brown bear AND a grizzly
NSPredicate *hasBrownAndGrizzlyPred = [NSPredicate predicateWithFormat:@"ANY Bear.color == 'brown' AND ANY Bear.breed == 'grizzly'"];


推荐答案

http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSExpression_Class/Reference/NSExpression.html#//apple_ref/doc/uid/TP30001190-SW22rel =nofollow noreferrer > SUBQUERY 谓词表达式。在对熊的查询的情况下(其中 Den Bear 具有一对多的关系, code> bears )是棕色的和灰熊的:

You can do this with a SUBQUERY predicate expression. In the case of a query for dens with a bear (where Den has a to-many relationship to Bear named bears) that is both brown and a grizzly:

[NSPredicate predicateWithFormat:@SUBQUERY (bears,$ b,$ b.color =='brown'AND $ b.breed =='grizzly')。@ count> 0];

这篇关于NSPredicate在一对多关系上引用多个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 20:46