Ios NSXMLParserDelegate没有响应
我正在尝试使用中描述的方法在objective-C中开发XML解析器 我已经对整个流程进行了编码,但是委托回调方法就是不响应 请看一下下面的代码块,并告诉我您是否可以找出任何错误 正在从以下位置调用分析器:Ios NSXMLParserDelegate没有响应,ios,objective-c,xcode,Ios,Objective C,Xcode,我正在尝试使用中描述的方法在objective-C中开发XML解析器 我已经对整个流程进行了编码,但是委托回调方法就是不响应 请看一下下面的代码块,并告诉我您是否可以找出任何错误 正在从以下位置调用分析器: NSString* filePath = [[NSBundle mainBundle] pathForResource:@"cache" ofType:@"xml"]; NSLog(@"Path location is : %@",filePath); NSData* xmlData = [
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"cache" ofType:@"xml"];
NSLog(@"Path location is : %@",filePath);
NSData* xmlData = [NSData dataWithContentsOfFile:[NSURL URLWithString:filePath]];
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlData];
if(nsXmlParser!=NULL)
{
NSLog(@"parser is %@",nsXmlParser);
}
HDDataXML *parser = [[HDDataXML alloc] initXMLParser];
[nsXmlParser setDelegate:parser];
BOOL success = [nsXmlParser parse];
// test the result
if (success)
{
NSLog(@"No errors - effects count : i");
} else
{
NSLog(@"Error parsing document!");
}
我在这里看到的只是错误解析文档!filePath变量是OK,并且解析器不是null
现在,在代理的.h文件中:
#import <Foundation/NSObject.h>
#import "EffectsCache.h"
@class EffectsCache;
@interface HDDataXML : NSObject<NSXMLParserDelegate>
{
EffectsCache *effectsHandler;
NSMutableString *currentElementValue;
NSMutableArray *effects;
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string ;
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName;
- (HDDataXML *) initXMLParser;
@property (nonatomic, retain) EffectsCache *effectsHandler;
@property (nonatomic, retain) NSMutableArray *effects;
@end
关键是,回调方法不起作用
请帮我找到错误
提前谢谢
nsXmlParser.delegate = self;
[nsXmlParser parse];
不要忘记在.h文件中调用委托
<NSXMLParserDelegate>
好的。。。我终于解决了它: 根据Martin R的建议,我更改了调用代理的代码:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"effects_cache" ofType:@"xml"];
NSLog(@"Path location is : %@",filePath);
NSData* xmlData = [NSData dataWithContentsOfFile:filePath];
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:xmlData];
在进行此修改之前,xmlData为null
还需要另一个小的修改:
更改:
[effectsHandler添加对象:效果]
到
为什么要将NSXMLParser委托方法声明到.h文件中?这是您的实际代码吗?在
[[NSXMLParser alloc]initWithContentsOfURL:xmlData]
中,使用NSData
对象作为URL参数。我希望它会立即崩溃您可能是指[[NSXMLParser alloc]initWithData:xmlData]代码>不,它没有在那条线上崩溃。但我确实将其更改为initWithData:xmlData,但仍然没有得到任何结果!您是否获得了NSLog(@“parser is%@”,nsXmlParser)
输出?是的,o/p是:parser很抱歉,但我认为这并不能回答问题。允许使用不同的对象作为XML解析器的委托;好啊我需要正确的语法:当我使用接口HDDataXML:NSObject时,程序会编译并运行,但什么都不会发生。当我使用接口HDDataXML:NSObject时,程序不会编译。如果我使用接口HDDataXML,程序将编译、运行并抛出一个有趣的警告:**NSForwarding:警告:类“HDDataXML”的对象0x11b5b0未实现methodSignatureForSelector:--出现故障并崩溃。这纯粹是一个语法相关的问题(我猜)。谁来解释一下!
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"effects_cache" ofType:@"xml"];
NSLog(@"Path location is : %@",filePath);
NSData* xmlData = [NSData dataWithContentsOfFile:filePath];
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:xmlData];
[effects setValue:currentElementValue forKey:elementName];