向后退后的UISearchBar错误

时间:2014-02-15 11:04:17

标签: ios objective-c uitableview uisearchbar

我创建了一个UISearchBar,它第一次进入viewController时工作,但是当我进入下一个viewcontroller时。 UITableView并按回来并尝试再次搜索我得到以下错误:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<phraseObject 0xa72d8c0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.'

以下是代码:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    if ([elementName isEqualToString:@"language"]) {
        thename = attributeDict[@"name"];
        theimage = attributeDict[@"image"];
        theId = attributeDict[@"id"];

        object = [finalObject new];
        theObject = [countryObject new];

        object.name = thename;
        object.image = theimage;
        object.soId = theId;

        theObject.name = thename;
        theObject.image = theimage;
        theObject.soId = theId;

        [finalArray addObject:object];
        [finalArray2 addObject:theObject];

    }
}

-(void) searchThroughdata {

    self.filteredArray = nil;

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains [c] %@",self.searchBar.text];
    self.filteredArray = [[finalArray filteredArrayUsingPredicate:predicate] mutableCopy];

}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    [self searchThroughdata];
}


- (void)viewDidLoad
{
    [super viewDidLoad];

        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
   height = 30.0;
searchBar.barTintColor = [UIColor clearColor];

    self.tableViewData.sectionIndexTrackingBackgroundColor = [UIColor clearColor]; // background color while the index view is being touched
    if ([self.tableViewData respondsToSelector:@selector(sectionIndexBackgroundColor)]) {
        self.tableViewData.sectionIndexBackgroundColor = [UIColor clearColor];   
    }
    tableViewData.backgroundColor = [UIColor clearColor];
    tableViewData.opaque = NO;
    tableViewData.backgroundView = nil;

    NSString* path = [[NSBundle mainBundle] pathForResource: @"lists" ofType: @"xml"];
    NSData* data = [NSData dataWithContentsOfFile: path];


    rows = [[NSMutableArray alloc] init];
    finalArray = [[NSMutableArray alloc] init];
    finalArray2 = [[NSMutableArray alloc] init];

    NSXMLParser* parser = [[NSXMLParser alloc] initWithData: data];

    [parser setDelegate:self];
    [parser parse];    

}

2 个答案:

答案 0 :(得分:1)

问题出在:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains [c] %@",self.searchBar.text];

编译器无法找到SELF.name

您在哪里搜索?对象键的名称是什么?

答案 1 :(得分:0)

  

由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[valueForUndefinedKey:]:此类不是密钥值的密钥值编码。'

似乎只有调试可以帮助解决这个问题。您应该尝试将“异常断点”设置为ON - 通过停止引发异常的应用程序可能有助于在代码中找到问题。

How to set exception breakpoint