[UInt8]
is not NSData
, so you can't use the NSString(data...
initializer
You might use
let chars: [UInt8] = [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0]
let count = chars.count / sizeof(UInt8)
let datastring = NSString(bytes: chars, length: count, encoding: NSASCIIStringEncoding)
In Swift 3 things have become much easier as a native String
can be initialized with a sequence of bytes.
let datastring = String(bytes: chars, encoding: .utf8)
However the example is not meaningful because it doesn’t represent a string so datastring
will be nil
. Use valid data like
let chars : [UInt8] = [72, 101, 108, 108, 111]
let datastring = String(bytes: chars, encoding: .utf8) // "Hello"