我有一个 NSArray
,其中的对象具有 name
属性。
我想通过 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)”
我该怎么做?我想过滤 name
中第一个字母为“A”的数组。
最佳答案
试试下面的代码
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF like %@", yourName];
NSArray *filteredArr = [yourArray filteredArrayUsingPredicate:pred];
已编辑:
NSPredicate
模式应该是:
NSPredicate *pred =[NSPredicate predicateWithFormat:@"name beginswith[c] %@", alphabet];
关于ios - 按字符串属性的首字母过滤数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18714634/