ios - Error ("'()' is not identical to 'UInt8'") writing NSData bytes to NSOutputStream using the write function in Swift -


I am trying to create an asynchronous file download in Swift. But I need to handle large files, so I found, understand.

However, I can not get it to work when I try to add NSData byte (write my NSURLConnection didReceiveData function) NSOutputStream to work, I get this error: '() 'UInt8' is not equivalent to doing this on line: bytesWritten = self.downloadStream.write (data.bytes, maxLength: bytesLeftToWrite).

data.bytes type is ConstUnsafePointer & lt; () & gt; and .write () function to ConstUnsafePointer & lt; UInt8 & gt; is expected, so in that sense, the error is correct but since I am new to iOS and definitely do SWIFT programming, how can I fix it?

So, how do I change the data.bytes? : ConstUnsafePointer & lt; () & Gt; to ConstUnsafePointer & lt; UInt8 & gt; alt Do this work any other way?

My didReiveiveData function:

  Function connection (connection: NSURLConnection !, didReceiveData Data: NSData) {var bytesLeftToWrite: NSInteger = bytesWritten at data.length: NSInteger = 0 while bytesLeftToWrite & gt; 0 {bytes = written: self.downloadStream.write (data.bytes, maxLength: bytesLeftToWrite) if bytesdrated == -1 {brake} bytes Left Titrate - = Bytes ResponseExiconlineGenate: NSNumber = NSNumber (Long Long: Self Download Response .experrent content length) NSNumber = NSNumber (long: data.length) self.downloadProgressPercentage = ((dataLength / responseExpectedlenght) * 100) println ("Downloaded: \ (self.downloadProgressPercentage)%")}}  

You can specify the with the insecure indicator Can be cast () :

  bytes written = self.downloadStream.write (unsafe indicator (data. Bytes), maximum length: bytes latehurite)  

There is also a problem in your writing loop, because you always should initial bytes of data objects in the output stream

This should probably look similar to (untest ed):

  bytes = UnsafePointer & lt; UInt8 & gt; (Data.bytes) bytesLeftToWrite: NSInteger = data.length while bytesLeftToWrite & gt; 0 {(bytes maxLength: bytesLeftToWrite) bytesWritten = go to self.downloadStream.write if bytesWritten == -1 {// break something went wrong ...} bytesLeftToWrite - = bytesWritten Bytes + = bytesWritten // Advance Pointer // }  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -