简体   繁体   中英

NSString from [NSData dataWithContentsOfURL:]

There is probably a really simple answer for this, but I can't seem to put my finger on it.

I am getting the contents of a web URL using (See Below)

NSData *data = [NSData dataWithContentsOfURL:webURL];

What I want to do is display this NSData in a readable form, not the hexadecimal representation.

This is also another way, bypassing the use of NSData altogether:

NSError *error = nil;
NSString *string = [NSString stringWithContentsOfURL:webURL encoding:NSUTF8StringEncoding error:&error];
NSString* newStr = [[NSString alloc] initWithData:data
                                         encoding:NSUTF8StringEncoding];

If this is just for display/logging, and you don't really need the string in your program, you can also set a breakpoint (click on the line numbers in the line you want to log).

Then right click the breakpoint you just created --> Edit breakpoint --> Click to add an action . Set the dropdown to debugger command , and in the Action text field, enter x/10s (char*)[data bytes] . This will write 10 "segments" of your data to the log. Check the Automatically continue... checkbox so the breakpoint doesn't halt the program.

Xcode4中的NSData日志记录断点

NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", response);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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