2012-10-02 51 views
0

我的應用程序使用一個由動態xml提要填充的表。我還使用pull來刷新Grant Paul編寫的代碼以重新加載包含新內容的表格(如果有的話)。第三,我正在使用Apple的Reachability在重新加載表格時確定互聯網連接。我的問題是這樣的;如果我打開應用程序與互聯網連接,然後當應用程序正在運行,關閉我的互聯網連接,然後我拉來刷新應用程序,它崩潰。當應用程序在Reachable和NotReachable之間切換時發生了一些情況。以下是我的代碼。該應用程序失敗,並在此行顯示線程6:SIGABRT信號:TFHppleElement * element = [elements objectAtIndex:0];iOS桌面刷新問題

以下是錯誤代碼:

2012-10-02 14:00:03.715 FireCom[2229:1517] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' 
*** First throw call stack: 
(0x2d63012 0x21a0e7e 0x2d050b4 0x603f 0xd810d5 0xd81034 0x96213557 0x961fdcee) 
libc++abi.dylib: terminate called throwing an exception 
(lldb) 

這裏是可達代碼:

- (void)loadCallList 
{ 
Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"]; 
NetworkStatus netStatus = [reach currentReachabilityStatus]; 

switch (netStatus) 
{ 
    case NotReachable: 
    { 
     NSLog(@"Access Not Available"); 

     [pull finishedLoading]; 
     [self displayInternetMessage]; 

     break; 
    } 

    case ReachableViaWWAN: 
    { 
     NSLog(@"Reachable WWAN"); 

     // Parse HTML for random ID and create XML link 

     NSURL *theURL = [NSURL URLWithString:@"http://www.wccca.com/PITS/"]; 
     NSData *data = [[NSData alloc] initWithContentsOfURL:theURL]; 
     xpathParser = [[TFHpple alloc] initWithHTMLData:data]; 
     NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']//@value"]; 
     TFHppleElement *element = [elements objectAtIndex:0]; 
     TFHppleElement *child = [element.children objectAtIndex:0]; 
     NSString *idValue = [child content]; 

     NSString *idwithxml = [idValue stringByAppendingFormat:@".xml"]; 
     NSString *url = @"http://www.wccca.com/PITS/xml/fire_data_"; 
     NSString *finalurl = [url stringByAppendingString:idwithxml]; 

     xmlParser = [[XMLParser alloc] loadXMLByURL:finalurl]; 

     if (xmlParser.calls.count == 0) 
     { 
      self.headerHeight = 0.0; 
      UIAlertView *noCalls = [[UIAlertView alloc] initWithTitle:@"No Current Calls" 
                   message:@"There are no current 9-1-1 calls in Clackamas or Washington County." 
                  delegate:self 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil]; 

      [noCalls show]; 
      [noCalls release]; 
     } 
     else 
     { 
      self.headerHeight = 45.0; 
     } 

     [callsTableView reloadData]; 
     [pull finishedLoading]; 

     break; 
    } 
    case ReachableViaWiFi: 
    { 
     NSLog(@"Reachable WiFi"); 

     // Parse HTML for random ID and create XML link 

     NSURL *theURL = [NSURL URLWithString:@"http://www.wccca.com/PITS/"]; 
     NSData *data = [[NSData alloc] initWithContentsOfURL:theURL]; 
     xpathParser = [[TFHpple alloc] initWithHTMLData:data]; 
     NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']//@value"]; 
     TFHppleElement *element = [elements objectAtIndex:0]; 
     TFHppleElement *child = [element.children objectAtIndex:0]; 
     NSString *idValue = [child content]; 

     NSString *idwithxml = [idValue stringByAppendingFormat:@".xml"]; 
     NSString *url = @"http://www.wccca.com/PITS/xml/fire_data_"; 
     NSString *finalurl = [url stringByAppendingString:idwithxml]; 

     xmlParser = [[XMLParser alloc] loadXMLByURL:finalurl]; 

     if (xmlParser.calls.count == 0) 
     { 
      self.headerHeight = 0.0; 

      UIAlertView *noCalls = [[UIAlertView alloc] initWithTitle:@"No Current Calls" 
                   message:@"There are no current 9-1-1 calls in Clackamas or Washington County." 
                  delegate:self 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil]; 

      [noCalls show]; 
      [noCalls release]; 
     } 
     else 
     { 
      self.headerHeight = 45.0; 
     } 

     [callsTableView reloadData]; 
     [pull finishedLoading]; 

     break; 
    } 
} 
} 
+3

你的代碼假定你的'elements'數組將有數據。您應該首先檢查是否爲零或計數== 0並添加邏輯以處理「無數據」情形。 – Jeremy

+0

元素數組將始終有數據。它永遠不會是空的。 –

+0

如果'xpathParser'找不到任何id爲'hidXMLID'的元素?我認爲答案在於數據。你沒有回覆你的想法。你的問題相當乾脆。元素中沒有任何東西,因此你得到錯誤。 – Jeremy

回答

1

我想我看到你的問題。

你不能假設elements具有至少1.


更新

也許你可以試試

NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']//@value"]; 
if (elements.count < 1) { 
    // Any needed cleanup 
    break; 
} 

TFHppleElement *element = [elements objectAtIndex:0]; 
TFHppleElement *child = [element.children objectAtIndex:0]; 

計數作爲一種快速出路。

+0

就像我上面所說的,元素數組總是會有數據!它解析一個HTML文件來查找一個ID,然後創建一個XML解析器可以用來解析的URL。 –