简体   繁体   English

将NSMutableData转换为NSString问题

[英]Converting NSMutableData to NSString Problem

initWithData does not convert my data object into a string properly. initWithData没有正确地将我的数据对象转换为字符串。 When I check the length of the data object, it has a value. 当我检查数据对象的长度时,它有一个值。

NSMutableData* receivedData =[[NSMutableData data] retain];

NSString* json_string = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];

Am I doing something wrong creating the string? 我在创建字符串时做错了吗?

As posted, the code is nonsense. 如发布,代码是无稽之谈。 You are creating an empty immutable data and then trying to create a string from said empty data. 您正在创建一个空的不可变数据,然后尝试从所述空数据创建一个字符串。

What does * When I check the length of the data object, it has a value* mean? 什么*当我检查数据对象的长度时,它有一个值* mean? Do you mean that you have more code that you aren't showing? 你的意思是你有更多的代码没有显示? Something that is filling the mutable data with some bytes? 用一些字节填充可变数据的东西?

Also, if the received data is not actually encoded as a UTF-8 string, the conversion will fail. 此外,如果接收的数据实际上没有编码为UTF-8字符串,则转换将失败。 There are a number of methods on NSString that allow for lossy conversion. NSString上有许多方法可以进行有损转换。 Try one of those. 尝试其中之一。

I didn't fully complete the NSURLConnection delegate methods. 我没有完全完成NSURLConnection委托方法。 This is where my data is being built. 这是我的数据构建的地方。

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

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