0

我正在尝试使用newTBXMLWithURL方法加载 xml 数据,一旦成功块返回 xml,我正在尝试使用委托来调度它,以便控制器接收NSMutableArray记录,但我一定做错了,我在控制台中收到一个错误,显示“PROGRAM RECEIVED EXC_BAD_ACCESS “我不确定我哪里出错了。下面附上代码

#import "XmlParser.h"
#import "TBXML+HTTP.h"
#import "NewsObject.h"

@implementation XmlParser
@synthesize  delegate = _delegate;

- (void)GetNewsList
{

    TBXMLSuccessBlock s = ^(TBXML *tbxml) {
        NSMutableArray *arrayOfNews;

        TBXMLElement *root = tbxml.rootXMLElement;

        TBXMLElement *newsListElement = [TBXML childElementNamed:@"NewsList" parentElement:root];

        TBXMLElement *newsElement = [TBXML childElementNamed:@"News" parentElement:newsListElement];

        while(newsElement !=nil){

            NewsObject *news = [[NewsObject alloc]init];

            news.headLine = [TBXML textForElement: newsElement ->firstChild];
            news.description = [TBXML textForElement:newsElement ->firstChild->nextSibling];
            news.imageUrl = [TBXML textForElement:newsElement->firstChild->nextSibling->nextSibling];

            if(arrayOfNews==nil)
                arrayOfNews = [NSMutableArray arrayWithObject:news];
            else
                [arrayOfNews addObject:news];

            newsElement = newsElement ->nextSibling;
        }

        [self.delegate XmlParser:self feedReady:arrayOfNews];
    };

    TBXMLFailureBlock f = ^(TBXML *tbxml, NSError *error) {
        NSLog(@"nay");
    };


    [TBXML newTBXMLWithURL:[NSURL URLWithString:@"url"]
                   success: s
                   failure: f];

}
@end

输入样本:

<xmlData>
<NewsList>
<News newsId="1" providerId="1" articleId="95020" sportId="6" sportName="RBL">
<Headline>Matai signs on with Manly</Headline>
<Description>
Manly has retained another one of its premiership stars with Steve Matai committing to the Sea Eagles until the end of the 2015 season.
</Description>
<Image>
http:google.com/All.png
</Image>
</News>
<News newsId="2" providerId="1" articleId="95019" sportId="7" sportName="RBU">
<Headline>Reds lose Lucas for Brumbies clash</Headline>
<Description>
Queensland has lost key utility back Ben Lucas to injury on the eve of Saturday night's vital match with the Brumbies at Canberra Stadium.
</Description>
<Image>
http:google.com/All.png
</Image>
</News>
</NewsList>
<xmlData>
4

1 回答 1

0

感谢您让我们知道实际的错误消息。该错误或警告没有单一原因。

另外,您是在使用 ARC 还是只是忘记了自动释放这些东西?您使用的是哪个 Xcode 版本和编译器?所有这些细节都很重要。

我会说你可以self在块内解决这个问题:

__block id _self = self;
TBXMLSuccessBlock s = ^(TBXML *tbxml) {
    /* use _self inside the block, not self */
};

请参阅https://stackoverflow.com/a/7854315/143097

上一个答案:

看来您正在调用一个不存在的方法:newTBXMLWithURL:success:failure:. 至少在我的 TBXML 版本中,它被称为:tbxmlWithURL:success:failure:.

我敢打赌,错误消息中的某处对此有提示,不是吗?

于 2012-05-27T10:26:31.373 回答