转换NSMutableData

时间:2011-07-26 12:22:10

标签: objective-c

如何从NSMutableData转换为NSMutableArray

 NSMutableData * webData=[[NSMutableData alloc] initWithData:self.sendRequestCompains];


- (NSMutableData *)sendRequestCompains{

    NSMutableString *sRequest = [[NSMutableString alloc] init];
    [sRequest appendString:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"];
    [sRequest appendString:@"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"];
    [sRequest appendString:@"<soap:Body>"];
    [sRequest appendString:@"<getCOMPAINs xmlns=\"http://tempuri.org/PlatformService/method\">"];
    [sRequest appendString:@"<Id_USR>"];    // any attribute
    [sRequest appendString:@"1"];    // attribute value
    [sRequest appendString:@"</Id_USR>"];
    [sRequest appendString:@"</getCOMPAINs>"];
    [sRequest appendString:@"</soap:Body>"];
    [sRequest appendString:@"</soap:Envelope>"];


    NSURL   *ServiceURL = [NSURL URLWithString:@"http://10.29.7.2/server_comp.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:ServiceURL];
    [request addValue:@"text/xml; charset:UTF-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"http://10.29.7.2/server_comp.php/getCOMPAINs" forHTTPHeaderField:@"SOAPAction"]; 
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[sRequest dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (conn) {
        myMutableData = [[NSMutableData data] retain];
        NSLog(@"Connection success");
        [NSURLConnection connectionWithRequest:request delegate:self];
        NSError *WSerror;
        NSURLResponse *WSresponse;

    myMutableData = [NSURLConnection sendSynchronousRequest:request returningResponse:&WSresponse error:&WSerror];
        NSLog(@"slt%d",[myMutableData length]);
    }

    return myMutableData;
}

1 个答案:

答案 0 :(得分:0)

由于您的数据来自您的网络服务器通过NSURLConnection,我建议将其转换为NSString并检查其结构:

NSString* dataStr = [[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"DATA: $@", dataStr);

你可能需要以某种方式解析结果(逐行?)并以这种方式构建数组。

如果您提供输出,则可以进一步帮助您。

OLD:

如果NSData对象最初是通过NSKeyedArchiver编码的数组,则可以尝试使用(docs):

[NSKeyedUnarchiver unarchiveObjectWithData:webData];

否则,请指明此NSData对象的来源及其创建方式。