如何过滤nsdictionary?
我想根据不同的条件从 nsdictionary 中搜索数据。假设我有 4 个键,用户可以根据任意数量的键进行搜索,例如:4,3 等。例如:我有包含键的字典(名字、姓氏、地址) ,电话号码)。现在用户可以根据任何键(如名字和姓氏,或仅名字)进行搜索。我正在使用 NSPredicate 进行搜索。我的问题是如何创建动态 nspredicate?如果我在中提供空字符串,
[NSPredicate predicateWithFormat:@"FirstName CONTAINS[cd] %@ AND LastNAme CONTAINS[cd] %@ AND Address CONTAINS[cd] %@ AND PhoneNumber CONTAINS[cd] %@",firstname,lastname,addr,phone]
则不会给任何结果。我怎样才能实现这个?或者我必须根据用户提供的字段创建多个 nspredicate?
提前致谢!
I want to search data from nsdictionary based on different criteria.Say I have 4 keys and user can search based on any number of keys ex:4,3 etc..Ex:I have dictionary containing keys(First Name,Last Name,Address,Phone Number).Now user can search based on any key say First Name and Last Name, or only First Name.I am using NSPredicate to search.My problem is how to create dynamic nspredicate?If I provide empty string in
[NSPredicate predicateWithFormat:@"FirstName CONTAINS[cd] %@ AND LastNAme CONTAINS[cd] %@ AND Address CONTAINS[cd] %@ AND PhoneNumber CONTAINS[cd] %@",firstname,lastname,addr,phone]
It does not give any result.How can I achieve this?or I have to create multiple nspredicate based on fields user has provide?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过编程方式构建谓词:
然后使用
filter
来过滤您的字典。You can build the predicate programmatically:
Then use
filter
to filter your dictionaries.