for (Person *person in personsOfInterest){ NSComparisonResult nameResult = [person.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0,[searchText length])]; if (nameResult == NSOrderedSame) { [self.filteredListContent addobject:person]; }}
不幸的是,此搜索仅匹配开头的文本.如果您搜索“John”,它将匹配“John Smith”和“Johnny Rotten”,但不匹配“Peach John”或“The John”.
有没有办法改变它,以便在名称中的任何地方找到搜索文本?谢谢.
解决方法 尝试使用rangeOfString:options:而不是:for (Person *person in personsOfInterest) { NSRange r = [person.name rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)]; if (r.location != NSNotFound) { [self.filteredListContent addobject:person]; }}
另一种可以实现此目的的方法是使用nspredicate:
nspredicate *namePredicate = [nspredicate predicateWithFormat:@"name CONTAINS[cd] %@",searchText];//the c and d options are for case and diacritic insensitivity//Now you have to do some dancing,because it looks like self.filteredListContent is an NSMutableArray:self.filteredListContent = [[[personsOfInterest filteredArrayUsingPredicate:namePredicate] mutablecopy] autorelease];//OR YOU CAN DO THIS:[self.filteredListContent addobjectsFromArray:[personsOfInterest filteredArrayUsingPredicate:namePredicate]];总结
以上是内存溢出为你收集整理的iphone – 搜索只是在开头匹配单词全部内容,希望文章能够帮你解决iphone – 搜索只是在开头匹配单词所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)