ios - RegEx: how to use regular expression with NSPredicate? -
i have table view display rows person's name property. table view allows user fussy search name. e.g. if user types in 'crp' match name 'chris paul'.
i have method takes search string , setup nspredicate search model's name in array
- (void)setpredicatewithtext: (nsstring *)searchtext { (int = 0; < searchtext.length; ++) { nsstring *character = [searchtext substringwithrange:nsmakerange(i, 1)]; self.format = [self.format stringbyappendingformat:@".*%@",character]; if (i == searchtext.length -1) // last character { self.format = [self.format stringbyappendingstring:@".*"]; } } // result format == .*c.*r.*p.* self.searchpredicate = [nspredicate predicatewithformat:@"self.name matches '%@'", self.format]; }
and in other method:
self.resultarray = [self.allplayers filteredarrayusingpredicate:self.searchpredicate];
but nothing comes result... please me, thanks!!
problem solved
there 2 things should change in code,
1) replace '%@' %@
2) add [c] ignore case sensitivity
the predicate should this,
[nspredicate predicatewithformat:@"self.name matches [c] %@", self.format];
finally method,
- (void)setpredicatewithtext: (nsstring *)searchtext { (int = 0; < searchtext.length; ++) { nsstring *character = [searchtext substringwithrange:nsmakerange(i, 1)]; self.format = [self.format stringbyappendingformat:@".*%@",character]; if (i == searchtext.length -1) // last character { self.format = [self.format stringbyappendingstring:@".*"]; } } // result format == .*c.*r.*p.* self.searchpredicate = [nspredicate predicatewithformat:@"self.name matches [c] %@", self.format]; }
Comments
Post a Comment