将 Int 复制到 UnsafeMutablePointer
Copy Int to UnsafeMutablePointer
我有一个 Int
,我想将其复制到 UnsafeMutablePointer<Int32>?
类型的指针
我试图将它转换为Data然后复制它,但是复制函数参数是UnsafeMutablePointer<UInt8>
类型
这是我试过的:
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
编辑:
我有以下函数,它获取一个指针。我需要将 int 复制到该地址。
func f(pointer: UnsafeMutablePointer<Int32>?) {
var numOfBytes = 100
//copy numOfBytes to the pointer's address
}
如果您只是想调用一个需要 UnsafeMutablePointer<Int32>
类型参数的函数,那么它就这么简单:
func f(_ i: UnsafeMutablePointer<Int32>) {
print(i.pointee)
}
var x = Int32()
f(&x)
我有一个 Int
,我想将其复制到 UnsafeMutablePointer<Int32>?
我试图将它转换为Data然后复制它,但是复制函数参数是UnsafeMutablePointer<UInt8>
这是我试过的:
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
编辑:
我有以下函数,它获取一个指针。我需要将 int 复制到该地址。
func f(pointer: UnsafeMutablePointer<Int32>?) {
var numOfBytes = 100
//copy numOfBytes to the pointer's address
}
如果您只是想调用一个需要 UnsafeMutablePointer<Int32>
类型参数的函数,那么它就这么简单:
func f(_ i: UnsafeMutablePointer<Int32>) {
print(i.pointee)
}
var x = Int32()
f(&x)