由于未捕获的异常终止应用程序' NSUnknownKeyException',原因:'此类不符合密钥值的密钥值编码

时间:2014-10-14 13:53:46

标签: objective-c uitableview uisearchdisplaycontroller

  

由于未捕获的异常“ NSUnknownKeyException ”而终止应用,   原因:'[< __ NSCFString 0x9c7aaf0> valueForUndefinedKey:]:这个类   对于密钥名称

不符合密钥值编码

我用这个错误搜索了整个stackoverflow但是我找到了解决方案,我认为我有不同的问题。

我正在尝试在我的项目中实现UISearchdisplaycontroller我有UITableview显示来自sqlite数据库的一些内容,并在显示后我想搜索我想要的但我无法这样做请一些帮助

这是我的代码:

-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
        NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
        searchResults = [jobTitleArray filteredArrayUsingPredicate:resultPredicate];`
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //int rowCount;
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {

    NSLog(@"Search count %d",[searchResults count]);
    return [searchResults count];


    } else
    {

    NSLog(@"jobTitleArray Search count %d",[jobTitleArray count]);
    return [jobTitleArray count];

    }
    //return rowCount;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    JobDetailViewController *jobD = [[JobDetailViewController alloc] init];`

    NSLog(@"Before: %@", jobD);

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [self performSegueWithIdentifier: @"showJobDetail" sender: self];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath`
{
    static NSString *CellIdentifier = @"Cell";
    //jobTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    jobTableCell *cell = (jobTableCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];`

    if (cell == nil) {
        cell = [[jobTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    JobDone *jobD = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {

        NSLog(@"job %@",searchResults);
        jobD = [searchResults objectAtIndex:indexPath.row];

    } else
    {
        jobD = [jobTitleArray objectAtIndex:indexPath.row];
    }
    cell.companyLabel.text=[companyArray objectAtIndex:indexPath.row];

    cell.titleLabel.text = [jobTitleArray objectAtIndex:indexPath.row];

    cell.descLabel.text=[descArray objectAtIndex:indexPath.row];
    return cell;
}


@end

1 个答案:

答案 0 :(得分:1)

您的错误消息会告诉您要查找的位置。它表示"此类不是键name"的键值编码兼容。那么,你在哪里使用namejobTitleArray似乎没有名为name的属性。

我没有看到jobTitleArray的样子,但如果它只是一个字符串数组,那么你可能想要这样的东西:

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"self contains[c] %@", searchText];

使用self而不是name


仔细查看代码,我推断出jobTitleArray必须是什么:字符串数组。

但我注意到您有单独的数组jobTitleArraycompanyArraydescArray。这将是一个问题,因为您根据其中一个阵列进行过滤。那么你将如何过滤其他人?!?

解决方案是将所有这三个保存在一个阵列中。例如,如果您有一个表单的数组:

NSArray *jobs = @[
                    @{@"name" : @"Manager of Purchasing", @"company" : @"Amazon", @"description": @"blah, blah, blah"}, 
                    @{@"name" : @"VP of Sales",           @"company" : @"Ebay",   @"description": @"blah, blah, blah"}
                 ];

然后,您的原始过滤器现在有效:

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];

如果您为"购买"过滤了以上内容,则您将获得亚马逊作业的完整字典条目。这使得所有三个属性保持彼此同步。

注意,这种技术同样适用于自定义对象,但希望这说明了基本思想:使用具有多个属性的单个对象数组,当您根据其中一个属性进行过滤时,您将得到所有对象他们的财产仍然很机智。

相关问题