Split UInt32 into [UInt8] in swift

Your loop can more compactly be written as

let byteArray = 24.stride(through: 0, by: -8).map {
    UInt8(truncatingBitPattern: example >> UInt32($0))
}

Alternatively, create an UnsafeBufferPointer and convert that to an array:

let example: UInt32 = 72 << 24 | 66 << 16 | 1 << 8 | 15

var bigEndian = example.bigEndian
let bytePtr = withUnsafePointer(&bigEndian) {
    UnsafeBufferPointer<UInt8>(start: UnsafePointer($0), count: sizeofValue(bigEndian))
}
let byteArray = Array(bytePtr)

print(byteArray) // [72, 66, 1, 15]

Update for Swift 3 (Xcode 8 beta 6):

var bigEndian = example.bigEndian
let count = MemoryLayout<UInt32>.size
let bytePtr = withUnsafePointer(to: &bigEndian) {
    $0.withMemoryRebound(to: UInt8.self, capacity: count) {
        UnsafeBufferPointer(start: $0, count: count)
    }
}
let byteArray = Array(bytePtr)

Improved @Martin R's answered. Worked on UInt16, UInt32, and UInt64:

protocol UIntToBytesConvertable {
    var toBytes: [Byte] { get }
}

extension UIntToBytesConvertable {
    func toByteArr<T: Integer>(endian: T, count: Int) -> [Byte] {
        var _endian = endian
        let bytePtr = withUnsafePointer(to: &_endian) {
            $0.withMemoryRebound(to: Byte.self, capacity: count) {
                UnsafeBufferPointer(start: $0, count: count)
            }
        }
        return [Byte](bytePtr)
    }
}

extension UInt16: UIntToBytesConvertable {
    var toBytes: [Byte] {
        return toByteArr(endian: self.littleEndian,
                         count: MemoryLayout<UInt16>.size)
    }
}

extension UInt32: UIntToBytesConvertable {
    var toBytes: [Byte] {
        return toByteArr(endian: self.littleEndian,
                         count: MemoryLayout<UInt32>.size)
    }
}

extension UInt64: UIntToBytesConvertable {
    var toBytes: [Byte] {
        return toByteArr(endian: self.littleEndian,
                         count: MemoryLayout<UInt64>.size)
    }
}

I had a similar need and was trying to learn a bit about manual memory management. Am I missing something, or has this gotten easier with newer versions of Swift? (I'm using Swift 5)

Creating an array of bytes ([UInt8]) from a UInt32

let example: UInt32 = 1

let byteArray = withUnsafeBytes(of: example.bigEndian) {
    Array($0)
}

print(byteArray) // [0, 0, 0, 1]

or if you want to append to an existing array, that can be done in the closure instead:

var existingArray: [UInt8] = [1, 2, 3]
let example: UInt32 = 1

withUnsafeBytes(of: example.bigEndian) {
    existingArray.append(contentsOf: $0)
}

print(existingArray)  // [1, 2, 3, 0, 0, 0, 1]