-1

I have an Int which I want to copy to a pointer of type UnsafeMutablePointer<Int32>?

I tried to cast it to Data and then copy it, but the copy function param is of type UnsafeMutablePointer<UInt8>

This is what I've tried:

var sizePointer: UnsafeMutablePointer<Int32>?
var numOfBytes = 100
var sizeData = Data(bytes: &(numOfBytes), count: MemoryLayout<Int>.size)
//What's now? How to copy it to the pointer?
//sizeData.copyBytes(to: size, count: MemoryLayout<Int>.size) not working

Edit:

I have the following function, that gets a pointer. I need to copy the int to that address.

func f(pointer: UnsafeMutablePointer<Int32>?) {
    var numOfBytes = 100
    //copy numOfBytes  to the pointer's address
}
Witterquick
  • 6,048
  • 3
  • 26
  • 50

1 Answers1

0

If you're just trying to call a function which expects an argument of type UnsafeMutablePointer<Int32>, then it's as simple as this:

func f(_ i: UnsafeMutablePointer<Int32>) {
    print(i.pointee)
}

var x = Int32()
f(&x)
Alexander
  • 59,041
  • 12
  • 98
  • 151