Ios 导致崩溃的SearchBarController谓词

Ios 导致崩溃的SearchBarController谓词,ios,objective-c,uisearchbar,nspredicate,uisearchdisplaycontroller,Ios,Objective C,Uisearchbar,Nspredicate,Uisearchdisplaycontroller,我正在尝试将searchBarController添加到我的tableView中。我在构建应用程序时没有收到任何错误,但当我开始在搜索栏中键入内容时,应用程序抛出异常并崩溃 导致崩溃的行在下面最后一段代码中进行了注释。有人知道这里可能出了什么问题吗?多谢 my.h文件的相关部分: @interface BusinessesViewController : UITableViewController<CLLocationManagerDelegate, UISearchDisplayDel

我正在尝试将searchBarController添加到我的tableView中。我在构建应用程序时没有收到任何错误,但当我开始在搜索栏中键入内容时,应用程序抛出异常并崩溃

导致崩溃的行在下面最后一段代码中进行了注释。有人知道这里可能出了什么问题吗?多谢

my.h文件的相关部分:

@interface BusinessesViewController : UITableViewController<CLLocationManagerDelegate, 
UISearchDisplayDelegate> {

    IBOutlet UITableView *mainTableView;
    NSArray *businesses;
    NSMutableData *data;
    NSArray *filteredBusinesses;
}
- (void)viewDidLoad {
    filteredBusinesses = [[NSArray alloc]init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [filteredBusinesses count];
    }
    else {
        return [businesses count];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {

    BusinessesTableViewCell *cell = (BusinessesTableViewCell *)[tableView 
    dequeueReusableCellWithIdentifier:@"MainCell"];

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [cell.businessMainLabel setText:[[filteredBusinesses 
        objectAtIndex:indexPath.row]objectForKey:@"name"]];
    }   
    else {
        [cell.businessMainLabel setText:[[businesses 
        objectAtIndex:indexPath.row]objectForKey:@"name"]];
    }

    return cell;
}
- (void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope {

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"beginswith[cd] %@", 
    searchText];

    //THIS LINE OF CODE CAUSES THE CRASH
    filteredBusinesses = [businesses filteredArrayUsingPredicate:predicate];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller 
    shouldReloadTableForSearchString:(NSString *)searchString {

    [self filterContentForSearchText:searchString scope:
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:
    [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}
行数分段方法:

@interface BusinessesViewController : UITableViewController<CLLocationManagerDelegate, 
UISearchDisplayDelegate> {

    IBOutlet UITableView *mainTableView;
    NSArray *businesses;
    NSMutableData *data;
    NSArray *filteredBusinesses;
}
- (void)viewDidLoad {
    filteredBusinesses = [[NSArray alloc]init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [filteredBusinesses count];
    }
    else {
        return [businesses count];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {

    BusinessesTableViewCell *cell = (BusinessesTableViewCell *)[tableView 
    dequeueReusableCellWithIdentifier:@"MainCell"];

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [cell.businessMainLabel setText:[[filteredBusinesses 
        objectAtIndex:indexPath.row]objectForKey:@"name"]];
    }   
    else {
        [cell.businessMainLabel setText:[[businesses 
        objectAtIndex:indexPath.row]objectForKey:@"name"]];
    }

    return cell;
}
- (void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope {

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"beginswith[cd] %@", 
    searchText];

    //THIS LINE OF CODE CAUSES THE CRASH
    filteredBusinesses = [businesses filteredArrayUsingPredicate:predicate];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller 
    shouldReloadTableForSearchString:(NSString *)searchString {

    [self filterContentForSearchText:searchString scope:
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:
    [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}
CellForRowAtIndexPath方法:

@interface BusinessesViewController : UITableViewController<CLLocationManagerDelegate, 
UISearchDisplayDelegate> {

    IBOutlet UITableView *mainTableView;
    NSArray *businesses;
    NSMutableData *data;
    NSArray *filteredBusinesses;
}
- (void)viewDidLoad {
    filteredBusinesses = [[NSArray alloc]init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [filteredBusinesses count];
    }
    else {
        return [businesses count];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {

    BusinessesTableViewCell *cell = (BusinessesTableViewCell *)[tableView 
    dequeueReusableCellWithIdentifier:@"MainCell"];

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [cell.businessMainLabel setText:[[filteredBusinesses 
        objectAtIndex:indexPath.row]objectForKey:@"name"]];
    }   
    else {
        [cell.businessMainLabel setText:[[businesses 
        objectAtIndex:indexPath.row]objectForKey:@"name"]];
    }

    return cell;
}
- (void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope {

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"beginswith[cd] %@", 
    searchText];

    //THIS LINE OF CODE CAUSES THE CRASH
    filteredBusinesses = [businesses filteredArrayUsingPredicate:predicate];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller 
    shouldReloadTableForSearchString:(NSString *)searchString {

    [self filterContentForSearchText:searchString scope:
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:
    [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}
谓词和SearchDisplayController方法:

@interface BusinessesViewController : UITableViewController<CLLocationManagerDelegate, 
UISearchDisplayDelegate> {

    IBOutlet UITableView *mainTableView;
    NSArray *businesses;
    NSMutableData *data;
    NSArray *filteredBusinesses;
}
- (void)viewDidLoad {
    filteredBusinesses = [[NSArray alloc]init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [filteredBusinesses count];
    }
    else {
        return [businesses count];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {

    BusinessesTableViewCell *cell = (BusinessesTableViewCell *)[tableView 
    dequeueReusableCellWithIdentifier:@"MainCell"];

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [cell.businessMainLabel setText:[[filteredBusinesses 
        objectAtIndex:indexPath.row]objectForKey:@"name"]];
    }   
    else {
        [cell.businessMainLabel setText:[[businesses 
        objectAtIndex:indexPath.row]objectForKey:@"name"]];
    }

    return cell;
}
- (void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope {

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"beginswith[cd] %@", 
    searchText];

    //THIS LINE OF CODE CAUSES THE CRASH
    filteredBusinesses = [businesses filteredArrayUsingPredicate:predicate];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller 
    shouldReloadTableForSearchString:(NSString *)searchString {

    [self filterContentForSearchText:searchString scope:
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:
    [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}

您的谓词错误,缺少密钥。从另一段代码来看
businesss
中的对象有一个“name”属性,因此谓词应该是

[NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@",  searchText]
                                   ^key   ^operator    ^value

谢谢你,马丁,我想我快到了。name属性来自JSON响应,如下所示:
[[businesss objectAtIndex:indexath.row]valueForKey:@“name”]
关于如何处理该属性,有什么想法吗?@Brandon:那么上面的方法应该有效。-请注意,
valueForKey:
通常是错误的方法(它用于键值编码),您上面的代码使用了
objectForKey:
,这是正确的。好的,谢谢您,现在只有在我输入一个确实在tableView中的字母时才会崩溃。如果我输入了tableView数组中不存在的内容,它会恰当地说“没有结果”。关于为什么仍然会崩溃还有其他想法吗?@Brandon:有关于崩溃的信息吗?消息,堆栈回溯胡乱猜测:试试
[self.tableView dequeueReusableCellWithIdentifier:@“MainCell”]
好了,不要再崩溃了。非常感谢。仍在行动,但仍在进步!谢谢