0

我的应用程序使用由动态 xml 提要填充的表。我还使用由 Grant Paul 编写的 pull to refresh 代码来重新加载新内容的表格(如果有的话)。第三,我在重新加载表格时使用 Apple 的可达性来确定互联网连接。我的问题是这个;如果我通过互联网连接打开应用程序,然后在应用程序运行时关闭我的互联网连接,然后我拉动以刷新应用程序并崩溃。当应用程序在 Reachable 和 NotReachable 之间切换时发生了一些事情。下面是我的代码。应用程序失败,线程 6:信号 SIGABRT 在此行:TFHppleElement *element = [elements objectAtIndex:0];

这是错误代码:

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;
    }
}
}
4

1 回答 1

1

我想我看到了你的问题。

NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']//@value"];
TFHppleElement *element = [elements objectAtIndex:0];
TFHppleElement *child = [element.children objectAtIndex:0];

您不能假设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];

作为一个快速的出路。

于 2012-10-02T21:07:53.430 回答