我想添加UInt32到我使用的字节缓冲区[UInt8].在java中,有一个方便的ByteBuffer类,它具有像putInt()这样的方法,就像这样.怎么可以在swift中完成?
我想我可以解决这个问题如下:
let example: UInt32 = 72 << 24 | 66 << 16 | 1 << 8 | 15
var byteArray = [UInt8](count: 4, repeatedValue: 0)
for i in 0...3 {
byteArray[i] = UInt8(0x0000FF & example >> UInt32((3 - i) * 8))
}
Run Code Online (Sandbox Code Playgroud)
这是非常冗长的,任何更简单的方法?