4
Core Audio的C API可以將一些數據複製到您提供的內存中。在一種情況下,我需要一個指針傳遞到一個AudioBufferList,其被定義爲:快速訪問可變長度數組
struct AudioBufferList {
var mNumberBuffers: UInt32
var mBuffers: (AudioBuffer) // this is a variable length array of mNumberBuffers elements
}
的UInt32的標識緩衝區的數目,實際的緩衝器緊隨。
我可以成功地得到這樣的:
let bufferList = UnsafeMutablePointer<AudioBufferList>.alloc(Int(propsize));
AudioObjectGetPropertyData(self.audioDeviceID, &address, 0, nil, &propsize, bufferList);
我不認識(AudioBuffer)語法,但我不認爲這是顯著 - 我認爲括號被忽略,mBuffers只是一個AudioBuffer和它的直到我做指針數學找到第二個。
我嘗試這樣做:
let buffer = UnsafeMutablePointer<AudioBuffer>(&bufferList.memory.mBuffers);
// and index via buffer += index;
// Cannot invoke 'init' with an argument of type 'inout (AudioBuffer)'
也試過:
let buffer = UnsafeMutablePointer<Array<AudioBuffer>>(&bufferList.memory.mBuffers);
// and index via buffer[index];
// error: Cannot invoke 'init' with an argument of type '@lvalue (AudioBuffer)'
措辭更一般地:在夫特,我怎樣才能採取UnsafeMutablePointer到一結構,並將其視爲那些結構的陣列?