Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String(data: x, encoding: NSUTF8StringEncoding) equivalent in Swift 3?

Tags:

swift3

In earlier versions of Swift I could convert NSData to a UTF8 string thus:

let desc:String = String(data: requestData, encoding: NSUTF8StringEncoding)

(The documentation for NSString's init(data:NSData,encoding:NSStringEncoding) is here.)

In Swift 3 using Data I would expect something like

let desc:String = requestData.description(usedEncoding: String.Encoding.utf8)

But that does not exist (Data's documentation is here). What is the correct Swift 3 translation?

like image 514
dumbledad Avatar asked Sep 22 '16 16:09

dumbledad


1 Answers

It's actually the same syntax

init?(data: Data, encoding: String.Encoding)


let desc = String(data: requestData, encoding: .utf8)

Don't annotate types the compiler can infer.

like image 81
vadian Avatar answered Sep 29 '22 11:09

vadian