The solution of this question no longer works with Swift 3.
There is no longer a property bytes of Data (formerly NSData.
let data = dataToWrite.first!
self.outputStream.write(&data, maxLength: data.count)
With this code, I get the error:
Cannot convert value of type 'Data' to expected argument type 'UInt8'
How can you write Data to an NSOutputStream in Swift 3?
NSDatahad abytesproperty to access the bytes. The newDatavalue type in Swift 3 has awithUnsafeBytes()method instead, which calls a closure with a pointer to the bytes.So this is how you write
Datato anNSOutputStream(without casting toNSData):Remarks:
withUnsafeBytes()is a generic method:In the above call, both
ContentTypeandResultTypeare automatically inferred by the compiler (asUInt8andInt), making additionalUnsafePointer()conversions unnecessary.outputStream.write()returns the number of bytes actually written. Generally, you should check that value. It can be-1if the write operation failed, or less thandata.countwhen writing to sockets, pipes, or other objects with a flow control.