开发者

converting data back into a string

开发者 https://www.devze.com 2023-01-10 14:04 出处:网络
So I have this: NSData *charlieSendData = [[charlieImputText stringValue] dataUsingEncoding:NSUTF8StringEncoding];

So I have this:

  NSData *charlieSendData = [[charlieImputText stringValue] dataUsingEncoding:NSUTF8StringEncoding];

I know how to convert NSStrings to data but how I convert data back to a开发者_如何学Cn NSString?

Elijah


You can use NSString's -initWithData:encoding: initializer method.

NSData *charlieSendData = [[charlieImputText stringValue] dataUsingEncoding:NSUTF8StringEncoding]
NSString *charlieSendString = [[NSString alloc] initWithData:charlieSendData encoding:NSUTF8StringEncoding];


Use NSString's initWithData method:

NSString *someString = [[NSString alloc] initWithData:charlieSendData encoding:NSUTF8StringEncoding];

That should do it.


Convert String to Data format

let data = string.data(using: .utf8)

Converting back from Data to string

 let str = String(data: data, encoding: String.Encoding.utf8) as String!

In Objective-c

NSString *string = [[NSString alloc] initWithData:dataToConvert encoding:NSUTF8StringEncoding];
0

精彩评论

暂无评论...
验证码 换一张
取 消