首页 > 解决方案 > 如何快速将 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

请有任何建议。我错过了什么?

标签: iosswiftoutputstream

解决方案


推荐阅读