NSPredicate:仅比较字符串的一部分?
我有一个 person_name###country_name 数组:
Bob###USA
Tim###UK
Sandy###German
我有一个 UISearchBar,它查找人名(即不考虑国家/地区),以下 NSPredicate 搜索人名以及国家/地区,我如何重新构造它,使其仅寻找人名?
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", __searchTerm];
NSArray *result = [self.source filteredArrayUsingPredicate:predicate];
person###country 数组是通过某种复杂的方式创建的,所以不建议我创建另一个仅包含人名的数组,而且,不同的国家可以有不同的同名人。
多谢!
I have an array of person_name###country_name:
Bob###USA
Tim###UK
Sandy###German
I have a UISearchBar which looks for person name(ie. does not consider country), the following NSPredicate searches person name as well as country, how do I re-construct it so it only looks for person name?
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", __searchTerm];
NSArray *result = [self.source filteredArrayUsingPredicate:predicate];
the array of person###country is created by some complex way, so do not suggest me create another array with only person names, also, different countries can have different people of the same name.
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
BEGINSWITH
而不是CONTAINS
。但实际上:如果两条数据是分开的,为什么要将它们存储在同一个字符串中?您应该将它们分开并创建一个具有两个属性的新类:
name
和country
。这将使所有这一切变得更加简单。You would use
BEGINSWITH
instead ofCONTAINS
.But really: if the two pieces of data are separate, why are you storing then in the same string? You should break them apart and create a new class with two properties:
name
andcountry
. That will make all of this much simpler.