3

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.

Community
  • 1
  • 1
Harikrishnan
  • 7,765
  • 13
  • 62
  • 113

4 Answers4

2

It's not a problem with debugger
The problem comes from compiler optimization, compiler see that string was not directly used, and optimizes the code by removing it and directly passing it to another method.

The key of the problem : You are running project on release scheme

Solution:
Here is a small guide to switch project to the Debug scheme

1) Click on the target, and click Edit scheme...

enter image description here

2) Popup will be displayed

enter image description here

3) Click Run %Your project%
4) Open Build Configuration popup
5) Select Debug
5) Press OK
6) You are ready to Go!, now you can debug anything :)

l0gg3r
  • 8,864
  • 3
  • 26
  • 46
1

If you are using ARC, and you just wrote the code that converts the data to a string and haven't written any code yet that actually uses the string, it will get deallocated immediately. Check whether that is what is happening. For example, what does NSLog (@"%@", html) display?

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Well, I am using the string right on the next line of `NSString *html = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];`, that is the allocating code. I tried setting a breakpoint on the line allocation(above line) and stepped into and found that the string is nil even after allocating, right in the next line of allocation. That definitely can't be ARC right? – Harikrishnan Jul 02 '14 at 10:17
0
NSAttributedString *str = [[NSAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
documentAttributes:nil error:&error];
Mahesh
  • 317
  • 1
  • 3
  • 17
-1

Try this one:

 NSString *myString = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];

Generally, conversion from NSData to NSString returns nil means there is mismatch between encoding format received from server and approach used for encoding.

Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
  • 1
    I had already tried this one. Just tried again. Not working. As I said, I tried with ALL encoding types. – Harikrishnan Jul 02 '14 at 09:42
  • Then there might be issue from response received from server end. Try using another response. – Jayprakash Dubey Jul 02 '14 at 09:47
  • I have already posted the `NSData` object along with the question. The response looks OK. And rather, it is the response I need to convert to string. Converting another response may work, but certainly won't help me. :) – Harikrishnan Jul 02 '14 at 09:51