NSPredicate - 无法解析格式字符串 [英] NSPredicate - Unable to parse the format string

查看:300
本文介绍了NSPredicate - 无法解析格式字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个 NSPredicate 来获取my_column值为 193e00a75148b4006a451452c618ccec 的行得到以下崩溃。


由于未捕获异常而终止应用程序
'NSInvalidArgumentException',原因:'无法解析格式
stringmy_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]]; 

和此

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

请帮助。



我发现这个,当我尝试与Martin R的回答

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



attributeName我传递一个'',所以我摘掉了attributeName并硬编码,然后

解决方案

将字符串用单引号括起来:

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


b $ b

更好,使用参数替换:

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

第二种方法避免了如果搜索字符串包含特殊字符
,例如'



备注:在建立谓词时不要使用 stringWithFormat stringWithFormat
predicateWithFormat 处理%K %@ ,所以结合这些
两个方法是非常容易出错(和不必要的)。


I am trying to write a NSPredicate to fetch rows with my_column value with this string "193e00a75148b4006a451452c618ccec" and I get the below crash.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "my_column=193e00a75148b4006a451452c618ccec"'

My predicate statement

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

also tried this

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

this

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

and this

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

Please help.

I found out this, when I was trying with Martin R's answer

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

attributeName I pass comes with a ' ' so I took off attributeName and hardcoded it, then it works fine.

解决方案

Enclose the string in single quotes:

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

or better, use argument substitution:

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

The second method avoids problems if the search string contains special characters such as ' or ".

Remark: Never use stringWithFormat when building predicates. stringWithFormat and predicateWithFormat handle the %K and %@ format differently, so combining these two methods is very error-prone (and unnecessary).

这篇关于NSPredicate - 无法解析格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆