NSPredicate不断崩溃我的应用程序

时间:2015-03-31 23:47:25

标签: ios objective-c parse-platform uisearchbar nspredicate

我创建了一个SearchBar,假设要过滤我设置的UITableView。我正在使用Parse.com将此数组拉入我的应用程序。

- (void)viewDidLoad {
     [super viewDidLoad];
     // Do any additional setup after loading the view.

     [self Query];

     //[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"SearchCell" bundle:nil] forCellReuseIdentifier:@"SearchData"];

}
- (void)didReceiveMemoryWarning {
     [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
}
-(void)Query {
     PFQuery *Q = [PFQuery queryWithClassName:@"_User"];

     [Q selectKeys:@[@"username"]];

     [Q findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
         if (!error) {
              NSLog(@"load Array);
              SearhArray = [[NSArray alloc] initWithArray:objects];    
         }
         else{ 
             NSLog(@"Array No Good");
         }
       }];
}

然后我尝试使用这些代码行过滤数组

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
     self.searchTerm = searchText;
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
     [self.mySearchBar resignFirstResponder];
     //reset to the original array and reload table
     self.filteredCities = SearhArray;
     [self.myTableView reloadData];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
     [self.mySearchBar resignFirstResponder];

     if ([self.searchTerm isEqualToString:@""]) {
         // Nothing to do here - Reset to the original array and reload table
         self.filteredCities = SearhArray;
     } else {
         // Filter the array based on the term searched for using a predicate
         NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.title contains[c] %@",self.searchTerm];
         self.filteredCities = [NSArray arrayWithArray:[SearhArray filteredArrayUsingPredicate:predicate]];
     }
     [self.myTableView reloadData];
} 

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
     self.filteredCities = SearhArray;
     [self.myTableView reloadData];
}  

但它一直在崩溃。我已将问题调试到此行

self.filteredCities = [NSArray arrayWithArray:[SearhArray filteredArrayUsingPredicate:predicate]];

我得到的错误是:

`2015-03-31 19:31:19.783 Pondu[59307:5149459] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Key "words" has no data.  Call fetchIfNeeded before getting its value.'
*** First throw call stack:
(0x18724a59c 0x1979a00e4 0x18724a4dc 0x1000c34bc 0x1880350f0 0x18807b934 0x18807b394 0x18807a084 0x188079e58 0x1000b31b0 0x18be207ac 0x18ba30d34 0x18ba19e48 0x18bbf91dc 0x18bbc3f04 0x18bd80890 0x18bd80540 0x18ba24950 0x18811ddf0 0x1872029ec 0x187201c90 0x1871ffd40 0x18712d0a4 0x1902cf5a4 0x18ba623c0 0x1000b1f64 0x19800ea08)
Libc++abi.dylib: terminating with uncaught exception of type NSException`

1 个答案:

答案 0 :(得分:1)

self.searchTerm看起来像NSString,但是你在那里放置的是你假设该值为NSDictionary。你需要使用像

这样的东西
  

@" SELF MATCHES%@"

,不是

  

@" SELF.title MATCHES%@"

相关问题