NSPredicate unable to parse format string

耗尽温柔 提交于 2019-12-04 08:20:17

You can do this:

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

Try change %1$@ to '%1$@':

NSString *formatString = [NSString stringWithFormat:@"(username CONTAINS[cd] '%1$@') || (userId CONTAINS[cd] '%1$@') || (firstname CONTAINS[cd] '%1$@') || (lastname CONTAINS[cd] '%1$@')", searchString];
NSPredicate *predicate = [NSPredicate predicateWithFormat:formatString];

You need to add the string for each variable, if you use 5 %@ you need to apply either the same searchString value or another value filling for that %@

[NSPredicate predicateWithFormat:@"(username CONTAINS[cd] %1$@) ||
(userId CONTAINS[cd] %1$@) || (firstname CONTAINS[cd] %1$@) ||
(lastname CONTAINS[cd] %1$@)", searchString1,searchString2,searchString3,searchString4]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!