【发布时间】:2016-01-04 07:02:31
【问题描述】:
我的核心数据中有一个字符串"0,1,2,3,4,5,6,7,8,9,10,11"(假定为月份索引字符串)。我想使用谓词来获取该字段是否包含数字,比如字符串是否包含“0”。
我们不能使用“CONTAINS”,因为“0”也出现在“10”中。我需要使用NSPredicate 获取对象以避免来自Core Data 的循环。
更新:
我只想使用NSPredicate 来测试'0,1,2,3,4,5,6,7,8,9,10,11' 是否包含'0'。
已解决
NSPredicate 的“MATCHES”运算符可用于与正则表达式进行比较:
NSString *searchTerm = @"0";
NSString *regex = [NSString stringWithFormat:@"(.*,)?%@(,.*)?", searchTerm];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"strIndex MATCHES %@", regex];
这对我来说很好。谢谢@skagedal 的建议。 我从Form NSPredicate from string that contains id's得到这个答案。
【问题讨论】:
-
让你的字符串为 00,01,02,03,04,05,06,07,08,09,10,11,12...
-
为什么你没有一个 Months 实体,然后从你的实体到 MOnths 有一对多关系?
标签: ios objective-c iphone ipad nspredicate