我有一个NSArray与具有name属性的对象.
我想按名称过滤数组
Nsstring *alphabet = [agencyIndex objectAtIndex:indexPath.section]; //---get all states beginning with the letter--- nspredicate *predicate = [nspredicate predicateWithFormat:@"SELF beginswith[c] %@",alphabet]; NSMutableArray *listSimpl = [[NSMutableArray alloc] init]; for (int i=0; i<[[Database sharedDatabase].agents count]; i++) { Town *_town = [[Database sharedDatabase].agents objectAtIndex:i]; [listSimpl addobject:_town]; } NSArray *states = [listSimpl filteredArrayUsingPredicate:predicate];
但是我收到一个错误 – “不能做一个不是字符串的东西的子串操作(lhs =< 1,Arrow> rhs = A)”
我该怎么做?我想过滤名字中第一个字母的数组为’A’.
解决方法
尝试以下代码
nspredicate *pred = [nspredicate predicateWithFormat:@"SELF like %@",yourName]; NSArray *filteredArr = [yourArray filteredArrayUsingPredicate:pred];
编辑:
nspredicate模式应该是:
nspredicate *pred =[nspredicate predicateWithFormat:@"name beginswith[c] %@",alphabet];