美文网首页
NSData 转化为NSString 失败解决办法

NSData 转化为NSString 失败解决办法

作者: hhao | 来源:发表于2017-03-13 10:37 被阅读0次

用以下方法将NSData转换NSString  结果为nil

NSString *dataString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

dataString =  [dataString stringByReplacingOccurrencesOfString:@" " withString:@""];

dataString =  [dataString stringByReplacingOccurrencesOfString:@"<" withString:@""];

dataString =  [dataString stringByReplacingOccurrencesOfString:@">" withString:@""];

返回值为nil原因:在使用initWithData等方法将NSData转换成NSString时,如果NSData的内容含有非encoding编码的字符,将会返回nil。

解决办法:

NSString *dataString = [data description];

dataString =  [dataString stringByReplacingOccurrencesOfString:@" " withString:@""];

dataString =  [dataString stringByReplacingOccurrencesOfString:@"<" withString:@""];

dataString =  [dataString stringByReplacingOccurrencesOfString:@">" withString:@""];

相关文章

网友评论

      本文标题:NSData 转化为NSString 失败解决办法

      本文链接:https://www.haomeiwen.com/subject/qfuvkttx.html