获取警告不兼容的指针类型从NSData分配给“NSMutableData”

时间:2012-05-09 10:16:59

标签: ios nsxmlparser nsdata

我收到警告::不兼容的指针类型在下面的代码中从'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];
}

2 个答案:

答案 0 :(得分:5)

使用

webData = [NSMutableData dataWithData:[str dataUsingEncoding:NSUTF16StringEncoding]];

答案 1 :(得分:2)

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

[webData appendData:[str dataUsingEncoding:NSUTF16StringEncoding]];