用以下方法将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:@""];