我有一个json对象进入
{
"c_id": "261",
"customer_id": "178729",
"name": "Three, Test"
},
{
"c_id": "261",
"customer_id": "178727",
"name": "Two, Test"
},
{
"c_id": "261",
"customer_id": "178728",
"name": "Two, Test"
},
{
"c_id": "261",
"customer_id": "185186",
"name": "Valid, Another"
},
{
"c_id": "261",
"customer_id": "183889",
"name": "White, Betty"
}
但是当此代码处理
时NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *tempCustomers = [[NSMutableArray alloc] init];
for (NSDictionary *dict in [json objectForKey:@"data"]) {
NSLog(@"dict: %@", dict);
NSLog(@"name: %@", [dict objectForKey:@"name"]);
[tempCustomers addObject:dict];
}
self.customers = tempCustomers;
NSLog(@"Customers: %@",customers);
self.customerData = [self partitionObjects:[self customers] collationStringSelector:@selector(self)];
NSLog(@"CustomerData: %@",customerData);
-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionCount = [[collation sectionTitles] count];
NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
for (int i = 0; i < sectionCount; i++) {
[unsortedSections addObject:[NSMutableArray array]];
}
for (id object in array) {
NSInteger index = [collation sectionForObject:[object objectForKey:@"name"] collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}
NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
for (NSMutableArray *section in unsortedSections) {
NSArray *sortedArray = [section sortedArrayUsingDescriptors:sortDescriptors];
//NSLog(@"Sort: %@",sortedArray);
//[sections addObject:[collation sortedArrayFromArray:section collationStringSelector:selector]];
[sections addObject:sortedArray];
}
return sections;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
NSLog(@"Search Display Controller: %@", self.customerData);
//NSString *predicateString = [NSString stringWithFormat:@"name CONTAINS[cd] '%@'",searchString];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] '%@'",searchString];
self.filteredCustomers = [[self.customerData filteredArrayUsingPredicate:predicate] mutableCopy];
NSLog(@"Filtered Customers %@", self.filteredCustomers);
return YES;
}
显示
2012-03-26 14:15:21.885 MyApp[65799:15003] name: Two, Test
2012-03-26 14:15:21.885 MyApp[65799:15003] dict: {
"c_id" = 261;
"customer_id" = 178728;
name = "Two, Test";
}
2012-03-26 14:15:21.885 MyApp[65799:15003] name: Two, Test
2012-03-26 14:15:21.885 MyApp[65799:15003] dict: {
"c_id" = 261;
"customer_id" = 185186;
name = "Valid, Another";
}
2012-03-26 14:15:21.885 MyApp[65799:15003] name: Valid, Another
2012-03-26 14:15:21.886 MyApp[65799:15003] dict: {
"c_id" = 261;
"customer_id" = 183889;
name = "White, Betty";
}
2012-03-26 14:15:21.886 MyApp[65799:15003] name: White, Betty
2012-03-27 08:35:24.764 MyApp[67330:fb03] Search Display Controller: (
(
),
(
{
"c_id" = 261;
"customer_id" = 178664;
name = "Test, My";
},
{
"c_id" = 261;
"customer_id" = 185182;
name = "Test, valid";
},
{
"c_id" = 261;
"customer_id" = 178729;
name = "Three, Test";
},
{
"c_id" = 261;
"customer_id" = 178727;
name = "Two, Test";
},
{
"c_id" = 261;
"customer_id" = 178728;
name = "Two, Test";
}
),
(
),
(
{
"c_id" = 261;
"customer_id" = 185186;
name = "Valid, Another";
}
),
(
{
"c_id" = 261;
"customer_id" = 183889;
name = "White, Betty";
}
),
(
),
(
),
(
),
(
)
)
2012-03-27 08:35:24.766 MyApp[67330:fb03] Filtered Customers (
)
注意名称键周围的“”缺失。我相信这就是我NSPredicate
searchDisplayController
无效的原因。为什么要删除引号以及如何修复searchDisplayController
工作?
答案 0 :(得分:2)
我只是注意到,在你的谓词中,你在%@周围有单引号 - 它们不应该在那里。将单引号放在%@周围会将其转换为文字。