Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSXMLParser与NSOperation一起工作不正常_Iphone_Objective C_Nsxmlparser_Nsoperation - Fatal编程技术网

Iphone NSXMLParser与NSOperation一起工作不正常

Iphone NSXMLParser与NSOperation一起工作不正常,iphone,objective-c,nsxmlparser,nsoperation,Iphone,Objective C,Nsxmlparser,Nsoperation,为了向服务器发送下载数据的请求,我使用了NSOperation。在收到数据后,我使用NSXMLParser来解析响应,但它没有调用解析器委托方法,例如 -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 或 谁能告诉我哪里做错了 //Creating NS

为了向服务器发送下载数据的请求,我使用了NSOperation。在收到数据后,我使用NSXMLParser来解析响应,但它没有调用解析器委托方法,例如

-(void)parser:(NSXMLParser *)parser didStartElement:(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委托方法中时,发现这些方法没有被调用

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


提前谢谢

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

//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];
//DataDownloadOperation.h
#进口
@接口数据下载操作:NSOperation
{
NSURL*目标URL;
}
@属性(保留)NSURL*targetURL;
-(id)initWithURL:(NSURL*)url;
@结束
//DataDownloadOperation.m
#导入“DataDownloadOperation.h”
#导入“MyXMLParser.h”
@数据下载操作的实现
@合成targetURL;
-(id)initWithURL:(NSURL*)url
{
如果(![super init])返回nil;
self.targetURL=url;
回归自我;
}
-(无效)解除锁定{
[targetURL发布],targetURL=nil;
[super dealoc];
}
-(无效)主要{
NSData*data=[NSData dataWithContentsOfURL:self.targetURL];
MyXMLParser*theXMLParser=[[MyXMLParser alloc]init];
n错误*错误=NULL;
[XmlParser解析XmlFileWithData:data parseError:&theError];
NSLog(@“Parse data1111:%@”,即xmlparser.mparseddct);
[XmlParser发行版];
}
@结束
//创建了如下操作:
NSURL*theURL=[NSURL-URLWithString:myurl];
NSOperationQueue*操作队列=[NSOperationQueue新建];
DataDownloadOperation*操作=[[DataDownloadOperation alloc]initWithURL:theURL];
[操作队列添加操作:操作];
[操作释放];

是否调用了
createRequestToGetData
?您有断点吗?请包含MyXMLParser类的代码。
parseXMLFileWithData:parseError:
是否创建一个NSXMLParser,将自身设置为委托,并调用NSXMLParser的解析方法?是否调用createRequestToGetData,MyXMLParser将自身设置为NSXMLParser委托。
//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];