[swift-users] Swift 3 version of creating a string from a pointer, length and encoding?

John Brownie john_brownie at sil.org
Thu Sep 29 04:47:58 CDT 2016


Next question in my migration. How do I do what this did in Swift 2.2?

let theData = String.init(bytes: data.memory, length: length, encoding: 
NSUTF8StringEncoding)

The only initialisers that have a length are

init?(bytesNoCopy: UnsafeMutableRawPointer, length: Int, encoding: 
String.Encoding, freeWhenDone: Bool)
init(utf16CodeUnitsNoCopy: UnsafePointer<unichar>, count: Int, 
freeWhenDone: Bool)

Neither is applicable.

Do I need to push it into a Data object first, like this?

let theData = String.init(data: Data.init(bytes: data, length: length), 
encoding: String.Encoding.utf8)

Or is there a better way that I've missed (highly possible)?

John
-- 
John Brownie
In Finland on furlough from SIL Papua New Guinea
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160929/18b640a8/attachment.html>


More information about the swift-users mailing list