我还有另一个新手问题。
我编写了一段代码,将NSString转换为NSMutableData,以模拟webService结果。
然而,事实证明cString已被弃用。你能帮我换一下吗? 这是我的代码。
NSString *testXMLDataString =
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
etc....
"</SOAP-ENV:Envelope>";
//Replace webData Received from the web service with the test Data
NSMutableData *testXMLData = [NSMutableData dataWithBytes:[testXMLDataString cString] length:[testXMLDataString length]];
[webData setData:testXMLData];
答案 0 :(得分:13)
NSData
方法创建+dataWithBytes:length:
对象。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];