XCode 6.1 iOS 8.1崩溃 - NSXMLParser不支持可重入解析

时间:2014-11-12 06:00:36

标签: ios8 nsxmlparser

我遇到了NSXMLParser的问题。我的源代码在iOS7 / XCode 5中运行良好,但在iOS8.1 / XCode 6中崩溃。崩溃错误是:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSXMLParser does not support reentrant parsing.'

我尝试使用this post中的其他解决方案,但错误仍然发生。任何人都可以帮助我更多?

我的资料来源

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate, NSXMLParserDelegate>
{
    NSXMLParser *xmlParser_;
...
}

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self parseData];
    ....
}

- (void)parseData
{
    titleList_ = [[NSMutableArray alloc] init];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:fDetail ofType:fXML];
    if (filePath)
    {
        NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        if (myText)
        {
            countPage_ = 2;
            NSData *xmlData = [myText dataUsingEncoding:NSUTF16StringEncoding];//NSUTF8StringEncoding];
            xmlParser_ = [[NSXMLParser alloc] initWithData:xmlData];
            xmlParser_.delegate = self;
            [xmlParser_ parse];

        }
    }
}

1 个答案:

答案 0 :(得分:1)

在iOS 8 / XCode 6中,相同的代码对我来说很好。 这是我的代码:

NSString *elementname;
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  [self parseData];
  return YES;
}

- (void)parseData
{
  titleList_ = [[NSMutableArray alloc] init];
  NSString *fDetail = [NSString stringWithFormat:@"sample"];
  NSString *filePath = [[NSBundle mainBundle] pathForResource:fDetail ofType:@"xml"];
  if (filePath)
  {
    NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    if (myText)
    {
      countPage_ = 2;
      NSData *xmlData = [myText dataUsingEncoding:NSUTF16StringEncoding];//NSUTF8StringEncoding];
      xmlParser_ = [[NSXMLParser alloc] initWithData:xmlData];
      xmlParser_.delegate = self;
      [xmlParser_ parse];

    }
  }
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
  elementname = elementName;
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
  elementname = elementName;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
  if([elementname isEqualToString:@"CatalogId"])
  {
      int64_t cId = [string longLongValue];
      NSLog(@"%lld",cId);
  }

}

@end