简体   繁体   English

从NSMutableData提取数据

[英]Extract data from NSMutableData

How can I extract the data from a NSMutabledata structure which returned by NSConection , like this: 我如何从NSConection返回的NSMutabledata结构中提取数据,如下所示:

<response>
TOKEN=abcdef
</response>

As the question is tagged iPhone i think you mean NSURLConnection and not NSConnection . 正如问题被标记为iPhone一样,我认为您的意思是NSURLConnection而非NSConnection My answer is based on that assumption. 我的答案就是基于这个假设。

You will have to implement the NSURLConnectionDelegate methods and get all the data into an NSMutableData object. 您将必须实现NSURLConnectionDelegate方法,并将所有数据放入NSMutableData对象。 I hope you have already done this as you have the NSMutableData . 希望您拥有NSMutableData已经完成此操作。

How to get data in the format you want depends on what type of data is being returned by the connection. 如何获取所需格式的数据取决于连接返回的数据类型。 If its an XML or JSON you will have to use a parser. 如果它是XML或JSON,则必须使用解析器。 An XML parser is available in objective c. 目标c中提供了XML解析器 If it is a sipmle string, you can directly convert it to a string in your connectionDidFinishLoading delegate method and use it: 如果它是sipmle字符串,则可以将其直接转换为connectionDidFinishLoading委托方法中的字符串并使用:

NSString *receivedDataString = [[NSString alloc] initWithData:receivedMutableData encoding:NSUTF8StringEncoding];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM