2017-02-27 37 views
-1

我有一個Int我想要複製到類型的指針UnsafeMutablePointer<Int32>?複製詮釋到UnsafeMutablePointer

我試圖將其轉換爲數據,然後將其複製,但複製功能參數是類型的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 
} 
+0

我編輯的問題 – Roee84

回答

1

如果你只是想打電話給其預計UnsafeMutablePointer<Int32>類型的參數的函數,那麼它就是這麼簡單:

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

var x = Int32() 
f(&x) 
+0

在那個例子中可以說我想將x複製到我指向的地址(其中我已經在其他地方進行了連接)。 – Roee84

+0

那麼相當於C代碼'* i = x;'?在Swift中,這將是'i.pointee = x' – Alexander

+0

是的,但我想要複製該值(因爲它在本地函數中定義)。 – Roee84