首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转换NSMutableData

转换NSMutableData
EN

Stack Overflow用户
提问于 2011-07-26 20:22:10
回答 1查看 927关注 0票数 0

如何将NSMutableData转换为NSMutableArray

代码语言:javascript
复制
 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;
}
EN

回答 1

Stack Overflow用户

发布于 2011-07-26 20:30:06

由于您的数据是通过NSURLConnection从您的web服务器获得的,因此我建议将其转换为NSString并检查其结构:

代码语言:javascript
复制
NSString* dataStr = [[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"DATA: $@", dataStr);

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

如果您提供输出,将有可能进一步帮助您。

旧的:

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

代码语言:javascript
复制
[NSKeyedUnarchiver unarchiveObjectWithData:webData];

否则,请指定此NSData对象的来源和创建方式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6829927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档