In that case I'd do something like this:
// using `in6_addr.init(_ string: String)` in this example
// from https://forums.swift.org/t/how-to-load-components-of-ipv4address/67739/5
var tuple = in6_addr("1:2:3:4:5:6:7:8").__u6_addr.__u6_addr8
var array = [UInt8](repeating: 0, count: MemoryLayout.size(ofValue: tuple))
withUnsafeBytes(of: &tuple) { tupleBuffer in
array.withUnsafeMutableBytes { arrayBuffer in
arrayBuffer.copyMemory(from: tupleBuffer)
}
}
print(array) // [0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8]
Alternative simpler version:
let array = withUnsafeBytes(of: tuple) {
[UInt8]($0)
}
A slightly modified version that uses return type inference and allows you specify desired target array type only once at the point of use:
// generic
func makeArray<T, R>(from tuple: R) -> [T] {
withUnsafeBytes(of: tuple) { buffer in
[T](buffer.assumingMemoryBound(to: T.self))
}
}
let array8: [UInt8] = makeArray(from: tuple)
print(array8) // [0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8]
let array16: [UInt16] = makeArray(from: tuple)
print(array16) // [256, 512, 768, 1024, 1280, 1536, 1792, 2048]