Objective-C将NSData转换为NSString [英] Objective-C Convert NSData to NSString
本文介绍了Objective-C将NSData转换为NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我尝试将NSData转换为NSString但代码返回程序接收信号:SIGABRT的代码。
Here are the code I tried to convert NSData to NSString but the program return "Program received signal:SIGABRT".
NSString *string= [NSString stringWithUTF8String:[data bytes]];
OR
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
还有其他更好的方法吗?
Is there any other better way to do it?
推荐答案
这里有一个很高的评价 - 投票答案,说明如何做到这一点。简而言之:
Here's a highly up-voted answer that shows how to do it. In a nutshell:
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
由于发送 [数据字节] <,第一个想法看起来像是失败了/ code>。 stringWithUTF8String没有为void *做好准备。第二个想法看起来好像应该有效,即使输入为零。
The first idea looks like a fail because of sending [data bytes]
. stringWithUTF8String isn't prepared for a void *. The second idea looks as if it should work, even with nil input.
这篇关于Objective-C将NSData转换为NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文