你需要做的是建立一个谓语为您的条款的每一个。例如,让我们打破你的查询:
- SELECT * FROM交易
- WHERE类别中(所属分类)
- 和位置(locationList)
- 和类型(TYPELIST)
- 和注释包含苯并[cd] “一些文本”
- AND DATE> = FROM日期AND DATE < + TODATE
基于此,您有5个谓词(2-6)。所以我们一个接一个地研究它们。
NSPredicate *inCategoryPredicate = [NSPredicate predicateWithFormat:@"Category IN %@", categoryList];
NSPredicate *locationPredicate = [NSPredicate predicateWithFormat:@"Location IN %@", locationList];
NSPredicate *typePredicate = [NSPredicate predicateWithFormat:@"Type IN %@", typeList];
NSPredicate *notePredicate = [NSPredicate predicateWithFormat:@"Note contains[c] %@", @"Some Text"];
NSPredicate *startDatePredicate = [NSPredicate predicateWithFormat:@"Date => @", fromDate];
NSPredicate *endDatePredicate = [NSPredicate predicateWithFormat:@"Date <= @", toDate];
现在,你只需要它们连接成只有一个谓语:Apple's documentation states:
你应该结构的复合谓词,以尽量减少做 的工作量。正则表达式匹配尤其是一个昂贵的操作 操作。在复合谓词中,因此,应该在正则表达式之前执行简单测试 ;
这就是说,你应该首先从“易”谓词开始。所以:
NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects: startDatePredicate, endDatePredicate, inCategoryPredicate, locationPredicate, typePredicate, notePredicate];
如果你使用NSLog,你总是可以知道谓词(sql where)的样子。
您是否尝试在谓词中输入where子句?它有一个方法来建立一个非常强大的字符串。 – 2010-10-17 23:15:04
我不知道任何让我指定一个生成谓词的SQL语句的地方。请指教。 – radesix 2010-10-18 22:53:49
我不知道为什么我的帖子不断被删除。这篇文章已经帮助了许多人http://www.radeeccles.com/convert-sql-statement-to-an-nspredicate-for-use-with-core-data/ – radesix 2013-03-13 16:29:18