我正在尝试将一个结构体传递到一个流中,以便将其发送到另一个设备的套接字上。代码可以运行,但发送的是错误的数据。每次都会发送随机数据,因此我肯定做错了什么。我在哪里出了问题?以下是我的代码:
public struct PStypes {
var u: UInt32 //< [X_XXXXXX V]
var i: UInt32 //< [X_XXXXXX A]
}
func sendMessage(message: String) {
var task = PStypes(u: 7, i: 9)
var bufferData = NSData(bytes: &task, length: 8)
var data:Data = bufferData as Data
var bufferDataSize = data.count
let bytesWritten = withUnsafePointer(to: &data) {
$0.withMemoryRebound(to: UInt8.self, capacity: bufferDataSize) {
outputStream.write($0, maxLength: bufferDataSize)
}
}
}