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

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

问题描述

我看不出这些代码行有任何问题。为什么不能解析?

I can't see any problem with these line of code. Why can't it parse?

[NSPredicate predicateWithFormat:@"(username  CONTAINS[cd] %1$@) || "
                                  "(userId    CONTAINS[cd] %1$@) || "
                                  "(firstname CONTAINS[cd] %1$@) || "
                                  "(lastname  CONTAINS[cd] %1$@)", searchString]"

日志也无济于事。


由于未捕获的异常而终止应用
'NSInvalidArgumentException',原因:'无法解析格式
string(用户名CONTAINS [cd]%1 $ @ )|| (userId CONTAINS [cd]%1 $ @)||
(名字CONTAINS [cd]%1 $ @)|| (姓氏CONTAINS [cd]%1 $ @)'

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "(username CONTAINS[cd] %1$@) || (userId CONTAINS[cd] %1$@) || (firstname CONTAINS[cd] %1$@) || (lastname CONTAINS[cd] %1$@)"'

编辑1:

好的,看起来似乎谓词谓词不会理解%1 $ @。我将其切换为

Okay, it seems like predicateWithFormat doesn't understand "%1$@". I switch it to

[... predicateWithFormat:[NSString stringWithFormat:...]] // (same format as above)

它通过了这条线。但是,下一个问题是:

It passed the line. But, the next problem is:

self.filteredUserList = [self.userList filteredArrayUsingPredicate:predicate];




由于未捕获的异常而终止应用程序' NSUnknownKeyException',
reason:'[valueForUndefinedKey:]:实体用户是
,而不是密钥值编码兼容的密钥a。'

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: the entity User is not key value coding-compliant for the key "a".'

a是我在searchTextBox中输入的关键字.WUT?

"a" is the keyword I entered in the searchTextBox. WUT?

我在调试控制台中打印出谓词,看起来没错:

I printed out the predicate in the debug console, looks nothing wrong:

username CONTAINS[cd] a OR userId CONTAINS[cd] a OR firstname CONTAINS[cd] a OR lastname CONTAINS[cd] a

编辑2:

好的,这个超级丑陋的代码解决了问题:

Okay, problem solved with this super ugly code:

[NSPredicate predicateWithFormat:@"(username  CONTAINS[cd] %@) || "
                                  "(userId    CONTAINS[cd] %@) || "
                                  "(firstname CONTAINS[cd] %@) || "
                                  "(lastname  CONTAINS[cd] %@)", searchString, searchString, searchString, searchString];

如果我想在将来扩展搜索字段怎么办?我必须添加更多参数?more,searchString,searchString,searchString?

What if I want to expand the search field in the future? I've got to add more parameters? more ", searchString, searchString, searchString"?

已解决

感谢Ewan和Bannings,为我的问题提供了两个选项。我测试了他们两个,他们的工作是一个魅力有人可以解释这两者之间的差异,在哪种情况下我应该使用哪个选项?

Thanks to Ewan and Bannings, giving 2 options to my question. I tested both of them, and they worked liek a charm. Can someone explain the different between those two, and in which case should I use which option?

**注**

Bannings的回答没问题,直到我的搜索字符串包含单引号',然后应用程序崩溃。所以我认为使用Ewan的更好。

Bannings' answer is alright, until my search string contains a single quote ', then the app crash. So I think use Ewan's one is better.

推荐答案

你可以这样做:

[[NSPredicate predicateWithFormat:@"(username CONTAINS[cd] $str) || ..."] predicateWithSubstitutionVariables:@{@"str": searchString}];

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

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