将数据转换回字符串

时间:2021-11-28 20:13:44

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 an NSString?

我知道如何将NSStrings转换为数据,但我如何将数据转换回NSString?

Elijah

以利亚

3 个解决方案

#1


63  

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

您可以使用NSString的-initWithData:encoding:initializer方法。

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

#2


6  

Use NSString's initWithData method:

使用NSString的initWithData方法:

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

That should do it.

应该这样做。

#3


0  

Convert String to Data format

将字符串转换为数据格式

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

Converting back from Data to string

从Data转换回字符串

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

#1


63  

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

您可以使用NSString的-initWithData:encoding:initializer方法。

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

#2


6  

Use NSString's initWithData method:

使用NSString的initWithData方法:

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

That should do it.

应该这样做。

#3


0  

Convert String to Data format

将字符串转换为数据格式

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

Converting back from Data to string

从Data转换回字符串

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