Ich habe ein JSON-ObjektDictionary-Objekt nicht Prädikat
{
"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"
}
jedoch in so kommt, wenn dieser Code verarbeitet
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;
}
Es zeigt
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 (
)
Beachten Sie die "" um der Name Schlüssel fehlt. Ich glaube, das ist der Grund, warum mein NSPredicate
in meinem searchDisplayController
nicht funktioniert. Warum werden die Anführungszeichen entfernt und wie würde ich meine searchDisplayController
reparieren?
Sind das alle Protokollanweisungen? Ich sehe keine von der searchDisplayController: shouldReloadTableForSearchString: Methode? Es scheint, dass diese Methode nicht aufgerufen wird. Haben Sie den Delegaten für den Suchcontroller festgelegt? Ich bin mir nicht sicher, ob Sie sich über die fehlenden Anführungszeichen Sorgen machen sollten, zumindest in OS X scheinen die Log-Anweisungen diese nach dem Zufallsprinzip zu belassen. – rdelmar
@rdelmar Ich habe die zusätzlichen Protokolle hinzugefügt. Natürlich habe ich sie abgeschnitten, um den anderen Abschnitten zu entsprechen. Sie sehen leere Arrays, die für die Kollatierungsabschnitte gelten, in denen sich keine Kunden befinden. – Bot