0

这里有很多使用“匹配”的解决方案,在这种情况下根本不起作用:

2013-08-20 10:30:40.890 ColonialChadstone[2609:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unimplemented SQL generation for predicate (SELF MATCHES "[0-9]+.*")'

最终解决方案应如下所示:

NSFetchedRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSString *predicateStr = [NSString stringWithFormat:@"SELF MATCHES '%@'", @"[0-9]+.*"];
[request setPredicate:predicateStr];
NSArray *resultArray = [context executeFetchRequest:request error:nil];
4

2 回答 2

2

(Valentin 是正确的,您应该在创建谓词时避免使用字符串格式化函数。它可能适用于您的情况,但通常容易出错,因为格式说明符和引用规则在stringWithFormat和之间有所不同predicateWithFormat。)

您的谓词的问题是您无法将托管对象本身(“SELF”)与模式进行比较,只能将托管对象的特定属性与模式进行比较:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"attribute MATCHES %@", @"[0-9]+.*"]
[request setPredicate:predicate];
于 2013-08-20T08:18:39.830 回答
0

尝试使用[request setPredicate:[NSPredicate predicateWithFormat:predicateStr]];而不是放NSString

于 2013-08-20T07:51:21.587 回答