-1

我正在尝试使用 Web 服务,但是当元素名称更改时,更改所有其他...

-(void)callWebService {
    _estacionamentoAdd = [[Estacionamento alloc]init];
    NSString *url = @"http://urlfromws";
    NSMutableURLRequest *request =[[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"GET"];

    NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (conn) {
        webData = [[NSMutableData data] retain];
    }
    else
    {

    }
}

//什么时候得到response的值

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if(!soapResults)
    {
        soapResults = [[NSMutableString alloc] init];
    }
    NSLog(@"stringFound:%@", string);
    [soapResults setString:string];
}

//收到响应时

   - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName        namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
 {
     if(!soapResults)
    {
        soapResults = [[NSMutableString alloc] init];
  }


if( [elementName isEqualToString:@"id"])
{
 id = [soapResults intValue];
}
else if( [elementName isEqualToString:@"name"])
{
    name = soapResults;
}
else if( [elementName isEqualToString:@"adress"])
{
    adress = soapResults;

 }
[soapResults setString:@""];

    }

当元素名称为“名称或地址”时,id 和名称等于地址

当 elementName 是 id...id = soapResults。好的,但是当 elementName 是 Adress 时,adress=soapResults 并且 id 也会改变。为什么?

4

2 回答 2

2

好吧,我只看到名称和地址将始终为 @"" 并且相同。

例如

...
name = soapResults; //NOT copied.. name and soapResults are the same object
... 
[soapResults setString:@""]; //affects name too, as they are the same 

地址相同,但 id 不同,因为那是整数,而不是对象

  • 名称 = 地址 = 肥皂结果...

您可能想要复制该值

name = [soapResults description];
...
address = [soapResults description];

其他问题:
不要在 foundCharacters 中使用 [soapResults setString:newString],使用 appendString(foundCharacters 被调用 n 次)

于 2012-12-01T20:34:09.563 回答
0

我解决了我的问题:

//总是释放 NSMutableString

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(soapResults) {
    [soapResults release];
    soapResults = nil;
}

    soapResults = [[NSMutableString alloc] init];

NSLog(@"stringFound:%@", string);
[soapResults appendString:string];

}

于 2012-12-03T11:18:18.670 回答