2012-10-02 93 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。 –