RaptureXML奇怪的行为

时间:2012-04-20 13:56:48

标签: objective-c ios xml xcode

我收到错误(好吧它没有显示,只是崩溃的应用程序,没有控制台上的信息) 每当我调用从RXML的rootXML迭代的方法时,似乎就会发生这种情况:

-(void)valueSearch {
    //FIRST CONNECTION
    NSString *serverAddress = @"http://www.commix.com.br/clientes/grupoglobo/apple/valor.xml";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverAddress] 
                                                       cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                   timeoutInterval:10];
NSError *requestError;
    NSURLResponse *urlResponse = nil;

    response = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];

    //SECOND CONNECTION - Just an encapsulated form of the first, since i use it in other parts
    // of the code
    response = [self requestWithParameters:@"valor.xml"];

    //i just uncommented both. but actually only one (connection) runs.

    //Creation of the rooXML so i can grab the info i need
    RXMLElement *rootXML = [RXMLElement elementFromXMLData:response];
    //This array is where i'll keep the info from the files.
    //it`s deallocated at the end in dealloc
    searchResult = [[NSMutableArray alloc] init];

    //This is the culprit. Atleast it seems so, since putting NSLog before and after
    //revealed so.
    [rootXML iterate:@"valor" usingBlock: ^(RXMLElement *valor) {
        NSLog(@"valor: %@", [valor child:@"nome"].text);
        [searchResult addObject:[valor child:@"nome"].text];
    }];
}

问题是,当我评论requestWithParameters并使用正常的非封装样式(// FIRST CONNECTION)时,我不会收到错误。但是,如果我使用第二个,当程序到达[rootXML iterate: [...]]时,它会在没有警告的情况下崩溃。

使用RaptureXML:https://github.com/ZaBlanc/RaptureXML

它也发生在代码的另一部分:

-(void)vehicleSearch {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"idArray" ofType:@"plist"];
    NSMutableArray *idArray = [[NSMutableArray alloc] initWithContentsOfFile:path];

    NSMutableString *serverAddress = (@"http://www.commix.com.br/clientes/grupoglobo/apple/modelo.php?marc=%@",[idArray objectAtIndex:0]);
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverAddress] 
                                                       cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                   timeoutInterval:10];

NSError *requestError;
    NSURLResponse *urlResponse = nil;
    response = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];

    RXMLElement *rootXML = [RXMLElement elementFromXMLData:response];
    searchResult = [[NSMutableArray alloc] init];

    [rootXML iterate:@"modelo" usingBlock: ^(RXMLElement *modelo) {
        NSLog(@"modelo: %@", [modelo child:@"nome"].text);
        [searchResult addObject:[modelo child:@"nome"].text];
    }];

    [idArray release];
}

发生在同一行[rootXML iterate:]

抱歉泄漏和东西,我没经验(这就是为什么我在这里),谢谢!

编辑: 实际上罪魁祸首就是这条线

NSMutableString *serverAddress = (@"http://www.commix.com.br/clientes/grupoglobo/apple/modelo.php?marc=%@",[idArray objectAtIndex:0]);

如果我直接传递参数,没有变量,它可以工作:

NSMutableString *serverAddress = (@"http://www.commix.com.br/clientes/grupoglobo/apple/modelo.php?marc=4");

它显示正确。

2 个答案:

答案 0 :(得分:1)

response = [self requestWithParameters:@"valor.xml"];

如果response是属性使用self.response,否则您将遇到内存泄漏问题。

答案 1 :(得分:1)

您确定,[idArray objectAtIndex:0]是NSString吗? 尝试使用

[NSString stringWithFormat:@"http://www.commix.com.br/clientes/grupoglobo/apple/modelo.php?marc=%@",[idArray objectAtIndex:0]];` 

甚至

[NSString stringWithFormat:@"http://www.commix.com.br/clientes/grupoglobo/apple/modelo.php?marc=%@",[[idArray objectAtIndex:0]stringValue]];