Sheehan Alam
Sheehan Alam

Reputation: 60919

Converting NSMutableData to NSString Problem

initWithData does not convert my data object into a string properly. 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?

Upvotes: 1

Views: 3925

Answers (2)

Sheehan Alam
Sheehan Alam

Reputation: 60919

I didn't fully complete the NSURLConnection delegate methods. This is where my data is being built.

Upvotes: 0

bbum
bbum

Reputation: 162722

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? 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. There are a number of methods on NSString that allow for lossy conversion. Try one of those.

Upvotes: 4

Related Questions