使用Swift中的write函数将NSData字节写入NSOutputStream时出错(“(()"与"UInt8""不同) [英] Error ("'()' is not identical to 'UInt8'") writing NSData bytes to NSOutputStream using the write function in Swift
问题描述
我正在尝试基于关于使用NSOutputStream代替NSData的答案是有意义的. /p>
但是,我无法正常工作.当我尝试将NSData字节(在我的NSURLConnection didReceiveData函数中)添加到NSOutputStream写入函数:'()' is not identical to 'UInt8'
的这一行:bytesWritten = self.downloadStream.write(data.bytes, maxLength: bytesLeftToWrite)
上时,出现此错误.
data.bytes
的类型为ConstUnsafePointer<()>
,并且.write()
函数期望该类型为ConstUnsafePointer<UInt8>
,因此从这种意义上说,错误是完全合理的.但是,由于我是iOS的新手,当然也不是Swift编程的新手,所以我无法集中精力解决此问题.
因此,如何将data.bytes: ConstUnsafePointer<()>
转换为ConstUnsafePointer<UInt8>
alt.通过其他方式进行这项工作?
我的didReceiveData
功能:
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
var bytesLeftToWrite: NSInteger = data.length
var bytesWritten: NSInteger = 0
while bytesLeftToWrite > 0 {
bytesWritten = self.downloadStream.write(data.bytes, maxLength: bytesLeftToWrite)
if bytesWritten == -1 {
break
}
bytesLeftToWrite -= bytesWritten
let responseExpectedlenght: NSNumber = NSNumber(longLong: self.downloadResponse!.expectedContentLength)
let dataLength: NSNumber = NSNumber(long: data.length)
self.downloadProgressPercentage = ((dataLength / responseExpectedlenght) * 100)
println("Downloaded: \(self.downloadProgressPercentage)%")
}
}
您可以使用UnsafePointer()
投射指针:
bytesWritten = self.downloadStream.write(UnsafePointer(data.bytes), maxLength: bytesLeftToWrite)
在写循环中也有一个问题,因为您总是写 数据对象的 initial 字节到输出流.
它可能看起来像这样(未经测试):
var bytes = UnsafePointer<UInt8>(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
// ...
}
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 NSOutputStream instead of NSData, makes sense.
However, I can't get it to work. I get this error when I try adding the NSData bytes (in my NSURLConnection didReceiveData function) to the NSOutputStream write function: '()' is not identical to 'UInt8'
on this row: bytesWritten = self.downloadStream.write(data.bytes, maxLength: bytesLeftToWrite)
.
data.bytes
is of the type ConstUnsafePointer<()>
and the .write()
function expects the type to be ConstUnsafePointer<UInt8>
, so in that sense, the error make perfect sense. But since I'm new to iOS and of course Swift programming, I can't get my head around how to fix this.
So, how do I convert the data.bytes: ConstUnsafePointer<()>
to ConstUnsafePointer<UInt8>
alt. make this work some other way?
My didReceiveData
function:
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
var bytesLeftToWrite: NSInteger = data.length
var bytesWritten: NSInteger = 0
while bytesLeftToWrite > 0 {
bytesWritten = self.downloadStream.write(data.bytes, maxLength: bytesLeftToWrite)
if bytesWritten == -1 {
break
}
bytesLeftToWrite -= bytesWritten
let responseExpectedlenght: NSNumber = NSNumber(longLong: self.downloadResponse!.expectedContentLength)
let dataLength: NSNumber = NSNumber(long: data.length)
self.downloadProgressPercentage = ((dataLength / responseExpectedlenght) * 100)
println("Downloaded: \(self.downloadProgressPercentage)%")
}
}
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<UInt8>(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
// ...
}
这篇关于使用Swift中的write函数将NSData字节写入NSOutputStream时出错(“(()"与"UInt8""不同)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!