How to initialise a string from NSData in Swift

2019-01-16 02:54发布

I have been trying to initialise a string from NSData in Swift.

In the NSString Cocoa Documentation Apple is saying you have to use this:

 init(data data: NSData!, encoding encoding: UInt)

However Apple did not include any example for usage or where to put the init.

I am trying to convert the following code from Objective-C to Swift

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

I have been trying a lot of possible syntaxes such as the following (of course it did not work):

var string:NSString!
string = init(data: fooData,encoding: NSUTF8StringEncoding)

7条回答
2楼-- · 2019-01-16 03:39
import Foundation
var string = NSString(data: NSData?, encoding: UInt)
查看更多
登录 后发表回答