NSString cString is Deprecated. What is the alternative?

强颜欢笑 提交于 2019-12-04 02:02:02
  1. Get the raw bytes from the string.
  2. Get the length of those bytes in the UTF8 encoding.
  3. Create the NSData object using the +dataWithBytes:length: method.

const char *rawBytes = [testXMLDataString UTF8String];
const NSUInteger length = [testXMLDataString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSAssert(length > 0, @"Couldn't convert to UTF-8");

NSMutableData *testXMLData = [NSMutableData dataWithBytes:rawBytes length:length];
[webData setData:testXMLData];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!