Dictionary对象不会谓词

问题描述:

我有一个JSON对象来作为Dictionary对象不会谓词

{ 
"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

这是所有的日志语句?我没有看到searchDisplayController中的一个:shouldReloadTableForSearchString:method?看来这个方法没有被调用。你是否设置了搜索控制器的代理?我不确定你应该担心缺失的引号,至少在OS X中,日志语句似乎会随机将它们关闭。 – rdelmar 2012-03-26 23:17:24

+0

@rdelmar我添加了额外的日志。当然,我将它们截断以匹配其他部分。你会看到空的数组,这些数组是没有任何客户的整理部分。 – Bot 2012-03-27 15:38:43

我刚刚注意到,在你的谓词中,你有%@的单引号 - 它们不应该在那里。将单引号放在%@周围将其变成文字。

+0

就是这样!谢谢! – Bot 2012-03-27 16:48:36