【发布时间】:2015-01-19 16:40:33
【问题描述】:
我有一个表格视图,我想通过可搜索的方式进行搜索。它以前可以工作,但是当我添加部分时,我遇到了麻烦,因为我必须从数组更改为字典。
所以基本上我有一个看起来像这样的 NSDictionary
{ @"districtA": array with point objects, @"districtB": array with point objects}
我需要根据数组中的点 objects.name 过滤它们。之后,我想创建一个包含过滤对象的新 nsdictionary。
我尝试了至少 10 种不同的方法,但我无法弄清楚,所以我认为这是我最积极的唯一方法。 这是我能想到的唯一方法,如果有更简单的方法或更逻辑的方法,请告诉我。
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name BEGINSWITH[c] %@",searchText];
//create new array to fill
NSArray *arrayWithFilteredPoints = [[NSArray alloc] init];
//loop through the values and put into an rray based on the predicate
arrayWithFilteredPoints = [NSArray arrayWithObject:[[self.PointList allValues] filteredArrayUsingPredicate:predicate]];
NSMutableDictionary *dict = [@{} mutableCopy];
for (Point *point in arrayWithFilteredPoints) {
if (![dict objectForKey:Point.district])
dict[Point.district] = [@[] mutableCopy];
[dict[Point.district]addObject:Point];
}
self.filteredPointList = dict;
self.filteredDistrictSectionNames = [[dict allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];}
这会导致崩溃,它当然发生在使用谓词的地方,但我不知道如何调试我应该使用什么谓词:
on 'NSInvalidArgumentException', reason: 'Can't do a substring operation with something that isn't a string (lhs = (
West ) rhs = w)'
【问题讨论】:
标签: ios objective-c xcode nsdictionary nspredicate