我有一个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];