我正在尝试构建查询字符串或谓词,但我一直收到此错误,
基本上我无法理解的是,这很好用:
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(name contains[cd] 'o')"];
但是这个:
NSString *predString = @"(name contains[cd] 'o')";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"%@", predString];
抛出这个:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "%@"'
答案 0 :(得分:12)
%@
不是谓词格式,它是字符串格式。
NSPredicate *pred = [NSPredicate predicateWithFormat:predString];
答案 1 :(得分:0)
正如所指出的,%@不能在谓词构造中的任何地方使用。但是能够提前构造谓词会很有用。您可以使用#define来定义它的全部或至少是常量部分:
#define kPredicateStr_MovieItem_MoviesInCatalog @"((wishList == NO) AND ((tvEpisode == NO) || (tvEpisode == YES AND (ANY tvSeries.genres.name LIKE 'TV Movie'))))"
然后按原样供应:
itemsViewController.predicate = [NSPredicate predicateWithFormat:kPredicateStr_MovieItem_MoviesInCatalog];
或添加到它:
NSString *predStr = [NSString stringWithFormat:@"%@ AND (%@ CONTAINS \"%@\")", kPredicateStr_MovieItem_MoviesInCatalog, titleForSearch, searchString];
predicate = [NSPredicate predicateWithFormat:predStr];