2012-06-28 33 views
-1

我正在我的NSMutableArray中進行某種搜索,但我經常獲得SIGABRT。我是新來的Objective-C,所以我不知道如果我錯過了一些重要的東西在這裏...當試圖比較某些字符串時獲取SIGABRT

-(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和sbWhat是UISearchBars

我在這兩個如果獲取SIGABRT聲明。我通過解析json字符串獲得的公司。

+1

我喜歡那些條件表達式 – 2012-06-28 19:07:52

+2

神聖的上帝,這是一個很大的邏輯OR的。 – CodaFi

+2

在SIGABRT之前打印到控制檯的整個錯誤消息是什麼?我打賭美元甜甜圈就像「發送到實例0xDEADBEEF的無法識別的選擇器」。 –

回答

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

+0

我在這裏發佈前檢查了nils 。我使用的JSON解析器將空字符串放在json字符串中缺少屬性的地方。 要儘快看看輸出。 – Eedoh