NSData 转化为NSString 失败解决办法

用以下方法将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 失败解决办法)