【问题标题】:NSXMLParser along with NSOperation is not working properlyNSXMLParser 和 NSOperation 无法正常工作
【发布时间】:2012-07-24 09:35:02
【问题描述】:

要向服务器发送请求以下载数据,我正在使用 NSOperation。接收数据后,我正在使用 NSXMLParser 解析响应,但它没有调用解析器委托方法等

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

谁能告诉我哪里做错了。

//Creating NSOperation as follows:
NSOperationQueue *operationQueue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(createRequestToGetData) object:nil]; 
[operationQueue addOperation:operation];
[operation release];    

-(void)createRequestToGetData
{
    NSError *error;
    NSURL *theURL = [NSURL URLWithString:myUrl];
    NSData *data = [NSData dataWithContentsOfURL:theURL];

    MyXMLParser *theXMLParser = [[MyXMLParser alloc]init];
    NSError *theError = NULL;
    [theXMLParser parseXMLFileWithData:data parseError:&theError];
    NSLog(@"Parse data:%@",theXMLParser.mParsedDict); //Cursor is not coming here.
    [theXMLParser release];
}

注意:MyXMLParser 是 NSObject 的子类,它实现 Parser 委托方法,但我的光标没有到达 NSLog。当在 Parser 委托方法中放置调试点时发现这些方法没有被调用。

谁能告诉我问题出在哪里以及如何解决这个问题。

提前致谢!

【问题讨论】:

  • createRequestToGetData 是否被调用?你那里有断点吗?
  • 请包含您的 MyXMLParser 类的代码。 parseXMLFileWithData:parseError: 是否创建了一个 NSXMLParser,将自己设置为委托,并调用 NSXMLParser 的解析方法?
  • 是的 createRequestToGetData 被调用并且 MyXMLParser 将自己设置为 NSXMLParser 委托。

标签: iphone objective-c nsxmlparser nsoperation


【解决方案1】:

我通过创建 NSOperation 的子类解决了上述问题,并在 NSOperation 子类的 main 方法中执行解析如下:

//DataDownloadOperation.h
#import <Foundation/Foundation.h>

@interface DataDownloadOperation : NSOperation
{
    NSURL *targetURL;
}
@property(retain) NSURL *targetURL;
- (id)initWithURL:(NSURL*)url;

@end


//DataDownloadOperation.m
#import "DataDownloadOperation.h"
#import "MyXMLParser.h"

@implementation DataDownloadOperation
@synthesize targetURL;

- (id)initWithURL:(NSURL*)url
{
    if (![super init]) return nil;
    self.targetURL = url;
    return self;
}

- (void)dealloc {
    [targetURL release], targetURL = nil;
    [super dealloc];
}

- (void)main {

    NSData *data = [NSData dataWithContentsOfURL:self.targetURL];
    MyXMLParser *theXMLParser = [[MyXMLParser alloc]init];
    NSError *theError = NULL;
    [theXMLParser parseXMLFileWithData:data parseError:&theError];
    NSLog(@"Parse data1111:%@",theXMLParser.mParsedDict);
    [theXMLParser release];
}
@end
//Created NSOperation as follows:
NSURL *theURL = [NSURL URLWithString:myurl];
        NSOperationQueue *operationQueue = [NSOperationQueue new];
        DataDownloadOperation *operation = [[DataDownloadOperation alloc] initWithURL:theURL];
         [operationQueue addOperation:operation];
         [operation release];

【讨论】:

    猜你喜欢
    • 2016-12-01
    • 2017-02-17
    • 2021-08-02
    • 2013-08-01
    • 2011-10-10
    • 2017-05-16
    • 2016-11-26
    • 2014-08-19
    • 2012-01-29
    相关资源
    最近更新 更多