1

我收到警告::不兼容的指针类型从下面的代码中的“NSData”分配给“NSMutableData”

-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[[NSString alloc] initWithBytes: [webData mutableBytes] length [webData length] encoding:NSUTF8StringEncoding] autorelease];

    theXML = [theXML stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    theXML = [theXML stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    NSLog(@"%@",theXML);

    if( xmlParser )
    {
        xmlParser = nil;
        [xmlParser release];
    }

    NSMutableString *str = [[NSMutableString alloc]initWithString:theXML];
    webData = [str dataUsingEncoding:NSUTF16StringEncoding];//WARNING

    xmlParser = [[[NSXMLParser alloc] initWithData:webData] autorelease];
    [xmlParser setDelegate:self];
    [xmlParser setShouldResolveExternalEntities: YES];
    [xmlParser parse];

    [connection release];
}
4

2 回答 2

5

采用

webData = [NSMutableData dataWithData:[str dataUsingEncoding:NSUTF16StringEncoding]];
于 2012-05-09T10:31:45.597 回答
2

您不能将 NSData 分配给 NSMutableData。NSMutableData 包含使它能够变异的逻辑,如果你将它指向一个 NSData 对象,这个逻辑就会消失。您应该做的是使用以下语法附加数据:

[webData appendData:[str dataUsingEncoding:NSUTF16StringEncoding]];
于 2012-05-09T10:22:23.337 回答