Before I installed Xcode 8 and converted project to Swift 3, the following line was fine. Now after conversion it looks like this:
let valueData:Data = Data(bytes: UnsafePointer<UInt8>(&intVal), count: sizeof(NSInteger))
it shows the error
Ambiguous use of 'init'
what is wrong with it in Swift 3? How to fix it?
UnsafePointer
has initializer for bothUnsafePointer
andUnsafeMutablePointer
, and sizeof was moved toMemoryLayout
disambiguate it as:The easiest way to create
Data
from a simple value is to go viaUnsafeBufferPointer
, then you don't need any explicit pointer conversion or size calculation:For a more generic approach for conversion from values to
Data
and back, see for example round trip Swift number types to/from Data.