在尝试比较某些字符串时获取SIGABRT

时间:2012-06-28 18:57:22

标签: iphone objective-c ios

我在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];
}

Btw,sbWhere和sb什么是UISearchBars

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

1 个答案:

答案 0 :(得分:3)

您不检查nil文字。如果您将nil传递给rangeOfString:,则会抛出异常。你也应该存储字符串,而不是多次调用相同的方法。

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) {
        ...

从现在开始,控制台也可能会回复一些您应该在帖子中包含的文字。 E.g:

  

*** 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