ios - 如何快速将 UInt32 类型的字节数组写入 OutputStream
问题描述
我正在尝试将 UInt32 类型的字节数组写入 OutputStream。我尝试过这样的事情:
var byteArr: Array<UInt8> = []
let header : [UInt32] = [30194250, 2, 3000, 0x782b415d, 0, 0]
for i in 0...5 {
var bigEndian = header[i].bigEndian
let count = MemoryLayout<UInt32>.size
let bytePtr = withUnsafePointer(to: &bigEndian) {
$0.withMemoryRebound(to: UInt8.self, capacity: count) {
UnsafeBufferPointer(start: $0, count: count)
}
}
byteArr.append(contentsOf: bytePtr)
}
print(byteArr)
outputStream?.write(UnsafePointer<UInt8>(byteArr), maxLength: byteArr.count)
但我得到一个错误2020-08-27 16:30:25.559888+0800 SocketConnectionTest[32297:26213693] SocketStream read error [0x6000020ce340]: 1 54 stream event NSStreamEvent(rawValue: 8) The operation couldn't be completed. Connection reset by peer stream event NSStreamEvent(rawValue: 8) The operation couldn't be completed. Connection reset by peer
请有任何建议。我错过了什么?
解决方案
推荐阅读
- javascript - 我们可以为对象键使用连接的值和变量吗?
- javascript - 使用 Jest 在 React 中测试输入数据
- javascript - HTML 如何阻止用户输入大于 2100-01-01 的日期?
- python - 延迟后如何重置键盘输入?
- excel - 向 Excel 列追溯添加“am”或“pm”
- python - Python子进程不断自行停止
- amazon-web-services - 使用 AWS PHP SDK 检测 EC2 角色?
- python-3.x - 在循环内使用 matplotlib 绘图
- java - Hibernate - 子类必须在它的母类之后绑定
- python - 如何确保对象实例属于同一类型(datetime.time)以便可以比较它们?