2015-03-31 47 views
0

我創建了一個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` 
+0

你的崩潰是在其他地方發生,可能是您從哪裏獲取解析數據和/或您當地的coredata商店 – Rog 2015-03-31 23:58:26

回答

1

self.searchTerm好像NSString的,但你放在那裏是你所假設值的NSDictionary 。你需要使用類似

@ 「SELF MATCHES%@」

,NOT

@ 「SELF.title MATCHES%@」

+0

該應用程序停止崩潰,但桌面視圖即將空白。所以我添加了一個NSLog來查看爲什麼NSLog(@「%@」,謂詞);它返回這個... 2015-03-31 20:21:20.610 Pondu [59863:5164017]自我匹配無 – Markinson 2015-04-01 00:23:10

+0

你能檢查self.searchTerm嗎?它不是零? – vichevstefan 2015-04-01 00:30:21

+0

其返回此.. 2015-03-31 20:37:10.088 Pondu [60019:5168434](null) – Markinson 2015-04-01 00:37:29