ios NSPredicate - 无法解析格式字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16358440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:26:50  来源:igfitidea点击:

NSPredicate - Unable to parse the format string

ioscore-datanspredicate

提问by Satheeshwaran

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

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

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

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无法解析格式字符串“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

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

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

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

我传递的 attributeName 带有一个“ ”,所以我去掉了 attributeName 并对其进行了硬编码,然后它就可以正常工作了。

回答by Martin R

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 stringWithFormatwhen building predicates. stringWithFormatand predicateWithFormathandle the %Kand %@format differently, so combining these two methods is very error-prone (and unnecessary).

备注:stringWithFormat在构建谓词时切勿使用。stringWithFormat并且 predicateWithFormat以不同的方式处理%K%@格式,因此将这两种方法结合起来非常容易出错(并且没有必要)。

回答by Thilina Chamin Hewagama

I think you have missed '' when checking for equality,

我认为您在检查相等性时错过了 '',

now try,

现在试试,

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

回答by Anupdas

Try

尝试

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

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