Mas*_*aro 42 iphone core-data nspredicate
我有一个Core Data模型,其中Task实体包含一个可选的to-many关系excludedOccurrences.excludedOccurrences的一个属性是start,它是一个NSDate对象.ExcludedOccurrence实体与Task实体具有反向强制一对一关系.
为了获取指定日期的任务,我需要确保指定的日期不会显示为任何ExcludedOccurrence实体的start属性.因此,我试图使用的一个子谓词
NSPredicate *occurrenceIsNotExcludedPredicate = [NSPredicate predicateWithFormat: @"(ALL excludedOccurrences.start != %@))", today];
Run Code Online (Sandbox Code Playgroud)
今天是今天的NSDate对象,仅包括日,月和年组件.所有排除的事件启动属性也仅包括日,月和年组件.
虽然这应该没问题,至少阅读Core Data和NSPredicate的文档,但是我收到以下错误消息:
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'不支持的谓词
如果我使用等效谓词
NSPredicate *occurrenceIsNotExcludedPredicate = [NSPredicate predicateWithFormat: @"!(ANY excludedOccurrences.start == %@))", today];
Run Code Online (Sandbox Code Playgroud)
没有抛出任何异常,但代码不能按预期工作:而是排除了今天不应排除的事件.
我不确定如何测试caseOccurrences == nil:以下谓词
NSPredicate *nilPredicate = [NSPredicate predicateWithFormat: @"(excludedOccurrences == nil)"];
Run Code Online (Sandbox Code Playgroud)
在运行时导致异常
这里不允许使用多个密钥
但是,由于excludedOccurrences关系是可选的,我还需要测试它是否为零.
我该如何处理?先感谢您.
Ash*_*ark 110
要测试空关系,您应该将to-many键的计数与零进行比较.
[NSPredicate predicateWithFormat:@"excludedOccurrences.@count == 0"];
Run Code Online (Sandbox Code Playgroud)
至于你的subpredicates,请注意你最终谓词中只能有一个ALL或多个ANY修饰符,尽管你可以在整个谓词中多次使用该修饰符.
不行:ANY foo.bar = 1 AND ALL foo.baz = 2
好的: ANY foo.bar = 1 AND !(ANY foo.baz != 2)
Mas*_*aro 28
在你们的帮助下,我终于设法为我的场景确定了正确的谓词.看起来NSDate对象被处理为double,但是,double从不像3.7,它总是像3.0因此,以下谓词在我的测试中正确工作:
NSPredicate *occurrenceIsNotExcludedPredicate = [NSPredicate predicateWithFormat: @"(excludedOccurrences.@count == 0 || (excludedOccurrences.@count > 0 && NONE excludedOccurrences.start == %@))",thisDate];
Run Code Online (Sandbox Code Playgroud)
其中thisDate是仅包含日,月和年组件的NSDate对象(如ExcludedOccurrence实体的start属性的情况).
正如Apple的一些人所建议的那样,基本上使用@count聚合运算符来测试空关系.
再次,非常感谢你的帮助.我仍然观察到文档在几个部分存在缺陷(特别是在它说ALL工作正常的情况下,相反,它根本不起作用).
因此,为了测试非空关系,这实际上是有效的:
[NSPredicate predicateWithFormat:@"relationship.@count != 0"]
Run Code Online (Sandbox Code Playgroud)
阿什利·克拉克给出的解决方案让我崩溃了"在这里不允许使用许多钥匙"
| 归档时间: |
|
| 查看次数: |
32809 次 |
| 最近记录: |