我創建了一個SearchBar,它可以過濾我設置的UITableView。我使用Parse.com將這個數組放入我的應用程序。NSPredicate不斷崩潰我的應用程序
- (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`
你的崩潰是在其他地方發生,可能是您從哪裏獲取解析數據和/或您當地的coredata商店 – Rog 2015-03-31 23:58:26