I know this question is asked before, but none of the solutions worked for me. I am trying to convert an NSData
object to a NSString
value. I am initing the NSString
object like following:
NSString *html = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
But the html
is always nil
. The NSData
I am trying to convert is the source code of a website. It is fairly long. This is 'NSData` I am trying to convert.
Is it the length of the data that is causing the issue? I need the source code as a string. What can I do to resolve this issue? What I tried so far:
- Tried with all encoding formats as shown in this answer.
- Tried with
[NSString stringWithUTF8String:[urlData bytes]];
But whatever I do produce the same result. html
always is nil
whatever I do.
EDIT
It was a problem with the debug console. Even when the objects had values in it, the debug console always showed nil as the value for most of the objects. However NSLog always displays the value.
NSData
object. Add some debugging to check length and disclose some of its content. If these look OK, then encoding is the next suspect. – trojanfoe