我正在尝试使用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>
答案 0 :(得分: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:
。
我打赌错误信息中的某处有一个提示,不是吗?