-1

我正在我的 NSMutableArray 中进行某种搜索,但我经常得到 SIGABRT。我是objective-c的新手,所以我不知道我是否错过了一些重要的东西......

-(IBAction)findButtonPushed:(id)sender
{
    Data *stuff = [Data getInstance];
    [stuff.results removeAllObjects];
    NSMutableArray *tempResults = [[NSMutableArray alloc] init];


    for (Company *company in stuff.companies) {
        if ([company.Description rangeOfString:[sbWhat text] options:NSCaseInsensitiveSearch].location != NSNotFound
            || [company.Name rangeOfString:[sbWhat text] options:NSCaseInsensitiveSearch].location != NSNotFound
            || [company.Url rangeOfString:[sbWhat text] options:NSCaseInsensitiveSearch].location != NSNotFound
            || [company.FirstName rangeOfString:[sbWhat text] options:NSCaseInsensitiveSearch].location != NSNotFound
            || [company.LastName rangeOfString:[sbWhat text] options:NSCaseInsensitiveSearch].location != NSNotFound
            || [company.Keywords rangeOfString:[sbWhat text] options:NSCaseInsensitiveSearch].location != NSNotFound) {
            [tempResults addObject:company];
        }
    }


    if (![[sbWhere text] isEqualToString:@""]) {
        for (Company *company in tempResults) {
            if ([company.Street rangeOfString:[sbWhere text] options:NSCaseInsensitiveSearch].location != NSNotFound
                || [company.City rangeOfString:[sbWhere text] options:NSCaseInsensitiveSearch].location != NSNotFound
                || [company.ZipCode rangeOfString:[sbWhere text] options:NSCaseInsensitiveSearch].location != NSNotFound
                || [company.State rangeOfString:[sbWhere text] options:NSCaseInsensitiveSearch].location != NSNotFound
                || [company.AddressPt1 rangeOfString:[sbWhere text] options:NSCaseInsensitiveSearch].location != NSNotFound
                || [company.AddressPt2 rangeOfString:[sbWhere text] options:NSCaseInsensitiveSearch].location != NSNotFound
                || [company.Country rangeOfString:[sbWhere text] options:NSCaseInsensitiveSearch].location != NSNotFound) {
                [stuff.results addObject:company];
            }
        }
    }
    else{
        stuff.results = tempResults;
    }


    [self performSegueWithIdentifier:@"ShowResults" sender:self];
}

顺便说一句,sbWhere 和 sb什么是 UISearchBars

我在这两个 if 语句上得到了 SIGABRT。我通过解析 json 字符串得到的公司。

4

1 回答 1

3

您不检查nil文本。如果传递nilrangeOfString:它将抛出异常。此外,您应该存储字符串而不是多次调用相同的方法。

NSString *what = [sbWhat text];
//Make sure what is not nil or empty
if([what length])
    for (Company *company in stuff.companies) {
        ...


NSString *where = [sbWhere text];
//Make sure where is not nil or empty
if ([where length]) {
    for (Company *company in tempResults) {
        ...

从现在开始,控制台也可能会反刍一些您应该在帖子中包含的文本。例如:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFConstantString rangeOfString:options:range:locale:]: nil argument' *** First throw call stack: (0x149c022 0x162dcd6 0x1444a48 0x14449b9 0x9360f6 0x96e17c 0x2a5f 0x2825 0x1) terminate called throwing an exception

于 2012-06-28T19:26:01.567 回答