I\'m trying to build an asynchronous file download in Swift based on the Erica Sadun\'s method. But I need it to handle bigger files, so I found this answer about using a NS
You can cast the pointer with UnsafePointer():
bytesWritten = self.downloadStream.write(UnsafePointer(data.bytes), maxLength: bytesLeftToWrite)
There is also a problem in your write loop, because you always write the initial bytes of the data object to the output stream.
It should probably look similar to this (untested):
var bytes = UnsafePointer(data.bytes)
var bytesLeftToWrite: NSInteger = data.length
while bytesLeftToWrite > 0 {
let bytesWritten = self.downloadStream.write(bytes, maxLength: bytesLeftToWrite)
if bytesWritten == -1 {
break // Some error occurred ...
}
bytesLeftToWrite -= bytesWritten
bytes += bytesWritten // advance pointer
// ...
}