Nested withUnsafeMutablePointer()

Since withUnsafeMutablePointers() has gone away, I typed out this example of using nested withUnsafeMutablePointer() so I was sure I knew what I was doing:

var int1 = UInt32(1)
var bytes1 = [UInt8(1), UInt8(2), UInt8(3)]
var int3 = UInt16(41)

withUnsafeMutablePointer(to: &int1) { int1Ptr in
	withUnsafeMutablePointer(to: &bytes1) { bytesPtr1 in
		withUnsafeMutablePointer(to: &int3) { int3Ptr in
			let _int3 = int3Ptr.pointee
			let _bytes3 = bytesPtr1.pointee
			let _int1 = int1Ptr.pointee
			Swift.print("\(_int3), \(_bytes3), \(_int1)")

The output, as expected, is “41, [1, 2, 3], 1”.

Leave a Reply

Your email address will not be published. Required fields are marked *