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

ios – NSPredicate – 无法解析格式字符串,第1张

概述参见英文答案 > Creating NSPredicate dynamically by setting the key programmatically                                    2 我正在尝试使用这个字符串“193e00a75148b4006a451452c618ccec”写入一个NSPredicate来获取带有my_column值的行,我得到以下崩 参见英文答案 > Creating NSPredicate dynamically by setting the key programmatically2
我正在尝试使用这个字符串“193e00a75148b4006a451452c618ccec”写入一个nspredicate来获取带有my_column值的行,我得到以下崩溃.

Terminating app due to uncaught exception
‘NSinvalidargumentexception’,reason: ‘Unable to parse the format
string “my_column=193e00a75148b4006a451452c618ccec”‘

我的谓词声明

fetchRequest.predicate=[nspredicate predicateWithFormat:[Nsstring stringWithFormat:@"%@==\"%@\"",attributename,itemValue]];

也试过了

fetchRequest.predicate=[nspredicate predicateWithFormat:[Nsstring stringWithFormat:@"%@ == %@",itemValue]];

这个

fetchRequest.predicate=[nspredicate predicateWithFormat:[Nsstring stringWithFormat:@"%@ = %@",itemValue]];

和这个

fetchRequest.predicate=[nspredicate predicateWithFormat:[Nsstring stringWithFormat:@"%@=\"%@\"",itemValue]];

请帮忙.

我发现这一点,当我试图用马丁R的答案

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

attributename我传来一个”,所以我拿掉了attributename和硬编码,然后它工作正常.

解决方法 将字符串括在单引号中:
[nspredicate predicateWithFormat:@"my_column = '193e00a75148b4006a451452c618ccec'"]

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

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

如果搜索字符串包含特殊字符,则第二种方法避免了问题
如“或”.

备注:建立谓词时切勿使用stringWithFormat. stringWithFormat和predicateWithFormat处理%K和%@格式不同,所以组合这些两种方法是非常容易出错(和不必要的).

总结

以上是内存溢出为你收集整理的ios – NSPredicate – 无法解析格式字符串全部内容,希望文章能够帮你解决ios – NSPredicate – 无法解析格式字符串所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1113453.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存