如何使用NSLog打印NSData对象的内容:
-(void) post:(NSString*) msg to:(NSString*) link{
NSString *myRequestString = [NSString stringWithFormat:@"message=%@", msg];
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: link]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: myRequestData];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSLog("%@", *returnData); //doesn't work
}
我想打印* returnData ...
的内容答案 0 :(得分:80)
使用
将NSData转换为NSStringNSString *strData = [[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
并在NSLog中打印NSString,如下所示
NSLog(@"%@",strData);
这个答案是为JeremyP编辑的,因为他不知道如何知道UTF-8的内容,尽管这不是对这个问题的讨论。
您可以在以下委托方法中获取响应标头
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *dic = [httpResponse allHeaderFields];
}
此词典将为您提供如下所示的完整标题信息
<CFBasicHash 0x5a45e40 [0x24b2380]>{type = immutable dict, count = 7,
entries =>
0 : <CFString 0x5d1bf60 [0x24b2380]>{contents = "X-Aspnet-Version"} = <CFString 0x5d21a60 [0x24b2380]>{contents = "2.0.50727"}
1 : <CFString 0x41a03a8 [0x24b2380]>{contents = "Server"} = <CFString 0x5d272f0 [0x24b2380]>{contents = "Microsoft-IIS/6.0"}
2 : <CFString 0x41a0010 [0x24b2380]>{contents = "Content-Length"} = <CFString 0x5d28630 [0x24b2380]>{contents = "385"}
6 : <CFString 0x419ff48 [0x24b2380]>{contents = "Cache-Control"} = <CFString 0x5d29c70 [0x24b2380]>{contents = "private, max-age=0"}
10 : <CFString 0x5d1c640 [0x24b2380]>{contents = "X-Powered-By"} = <CFString 0x5d26f10 [0x24b2380]>{contents = "ASP.NET"}
11 : <CFString 0x41a0060 [0x24b2380]>{contents = "Content-Type"} = <CFString 0x5d29c90 [0x24b2380]>{contents = "text/xml; charset=utf-8"}
12 : <CFString 0x41a0088 [0x24b2380]>{contents = "Date"} = <CFString 0x5d27610 [0x24b2380]>{contents = "Fri, 08 Jul 2011 15:23:10 GMT"}
}
检查charset =“utf-8”,您将从此处获得编码。
答案 1 :(得分:60)
如果你这样做:
NSLog(@"%@", returnData);
NSData将以十六进制格式记录。我想这可能就是你想要的。
如果要将其转换为字符串并记录字符串,首先需要找出使用的字符集。 HTTP的默认字符集不是UTF-8,而是ISO-8859-1 。一种方法是检查charset部分的Content-Type
标头。
答案 2 :(得分:4)
你必须考虑的一件事是:
NSLog(@"%@", *returnData); // this is wrong.
NSLog(@"%@", returnData); // this is correct.
我希望我能帮忙!
答案 3 :(得分:3)
我经常想看看NSData实际代表什么。通常它是某种文本,这使得十六进制有点不方便。因此,我通常在我的网络浏览器中的JavaScript控制台中编写此代码段,工作速度非常快,如果需要继续处理,可以轻松修改。
将以下脚本复制/粘贴到浏览器控制台中(右键单击此处 - &gt;检查元素),按Enter键
(function nsDataHexToString() {
var str = prompt("Paste the hex string here:", "ié. 48656c6c 6f207468 657265...")
var chs = str.replace(/[^A-F0-9]/ig,"").split("")
var res = ""
var cnt = 2
for (var i = 0; i+cnt-1<chs.length; i+=cnt) {
var nr = ""
for (var j=0; j<cnt; j++)
nr += chs[i+j]
nr = parseInt(nr, 16)
res += String.fromCharCode(nr)
}
console.log(res)
return res
})()
运行swift / obj-c代码,放入断点并检查NSData对象
let sample = "Hello there"
let data = sample.dataUsingEncoding(NSUTF8StringEncoding)
// Put breakpoint here, hover over "data", and press the eye/i
复制十六进制(类似于<48656c6c 6f207468 657265>
)并粘贴到浏览器提示中
最近,检查NSAttributedString.dataFromRange
的输出,rtfd使用了不同的编码,但我得到了我需要的东西:)对于某些json转换问题等也很有用。
答案 4 :(得分:1)
如果您需要将数据字节作为字符串
,请检查此答案