NSPredicate – 无法解析格式字符串

我正在尝试使用此字符串“ 193e00a75148b4006a451452c618ccec ”编写NSPredicate以获取具有my_column值的行,并且我得到以下崩溃。

因未捕获的exception’NSInvalidArgumentException’而终止应用程序,原因:’无法解析格式字符串“my_column = 193e00a75148b4006a451452c618ccec”’

我的谓词陈述

 fetchRequest.predicate=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@==\"%@\"",attributeName,itemValue]]; 

也尝试了这个

 fetchRequest.predicate=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@",attributeName,itemValue]]; 

这个

 fetchRequest.predicate=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ = %@",attributeName,itemValue]]; 

和这个

 fetchRequest.predicate=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@=\"%@\"",attributeName,itemValue]]; 

请帮忙。

当我尝试使用Martin R的答案时,我发现了这一点

 fetchRequest.predicate=[NSPredicate predicateWithFormat:@"%@==%@",attributeName,itemValue]; 

attributeName我传递带有”所以我取下了attributeName并硬编码了它,然后它工作正常。

将字符串括在单引号中:

 [NSPredicate predicateWithFormat:@"my_column = '193e00a75148b4006a451452c618ccec'"] 

或者更好 ,使用参数替换:

 [NSPredicate predicateWithFormat:@"my_column = %@", @"193e00a75148b4006a451452c618ccec"] 

如果搜索字符串包含诸如'"类的特殊字符,则第二种方法可以避免问题。

备注:在构建谓词时,切勿使用stringWithFormatstringWithFormatpredicateWithFormat处理%K%@格式,因此组合这两种方法非常容易出错(并且不必要)。

我认为你在检查平等时错过了”

现在试试,

 [NSPredicate predicateWithFormat:@"my_column='193e00a75148b4006a451452c618ccec'"]; 

尝试

 //Case Insensitive fetchRequest.predicate=[NSPredicate predicateWithFormat:@"%K = [cd] %@",attributeName,itemValue]; //Case sensitive fetchRequest.predicate=[NSPredicate predicateWithFormat:@"%K = %@",attributeName,itemValue];