我有这些文件:
XMLParser.h
#import <Foundation/Foundation.h>
@interface XMLParser : NSObject <NSXMLParserDelegate>
{
NSXMLParser *parser;
}
-(id)initWitData:(NSData *)data;
-(void)parseXML;
@end
XMLParser.m(部分内容)
#import "XMLParser.h"
@implementation XMLParser
- (id)initWitData:(NSData *)data
{
if (self = [super init]) {
parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
}
return self;
}
- (void)parseXML{
[parser parse];
};
ViewController.h
#import <UIKit/UIKit.h>
#import "XMLParser.h"
@interface ViewController : UIViewController <CLLocationManagerDelegate>
{
NSXMLParser *xmlToString;
}
@end
和ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:@"world" ofType:@"xml"];
NSData *data = [[NSData alloc]initWithContentsOfFile:xmlFilePath];
xmlToString = [[NSXMLParser alloc]initWithData:data];
//[xmlToString setDelegate:self];
[xmlToString setShouldResolveExternalEntities:YES];
[xmlToString parseXML]; // I am getting the error here.
}
任何人都可以帮助我吗?我在其他项目和它的工作中做了差不多!唯一的区别是initWithData
答案 0 :(得分:2)
您正在创建NSXMLParser
,然后尝试从XMLParser
调用方法。您确定并不是要将xmlToString
声明为XMLParser*
然后说
xmlToString = [[XMLParser alloc] initWithData:data];
当然,您对-setShouldResolveExternalEntities:
的调用将失败,因此您可能需要公开基础NSXMLParser
对象。