一般问题:我有一个对象要转换为constusafepointer。我所做的一切似乎都失败了…我该怎么做?我以前在dp2中可以做到这一点,但在dp3中无法解决。
以前,我有这样的代码:
var bpData = AUSamplerBankPresetData(bankURL: Unmanaged<CFURL>(_private: soundBankURL), bankMSB: UInt8(kAUSampler_DefaultMelodicBankMSB), bankLSB: UInt8(kAUSampler_DefaultBankLSB), presetID: presetID, reserved: 0)
let bpDataPointer: CConstVoidPointer = &bpData
// set the kAUSamplerProperty_LoadPresetFromBank property
result = AudioUnitSetProperty(samplerAudioUnit,
UInt32(kAUSamplerProperty_LoadPresetFromBank),
UInt32(kAudioUnitScope_Global),
0, bpDataPointer, 8)
但是,现在cconstvoidpointer不再存在,已被constunsafeinter替换。audioUnitSetProperty方法如下所示:
func AudioUnitSetProperty(inUnit: AudioUnit, inID: AudioUnitPropertyID, inScope: AudioUnitScope, inElement: AudioUnitElement, inData: ConstUnsafePointer<()>, inDataSize: UInt32) -> OSStatus
但是,如果将cconstvoidpointer更改为constunsafepointer我会得到错误:
“inout ausamplerbankpresetdata”不能转换为“constunsafepointer”
有人有什么想法吗?谢谢。
最佳答案
似乎不需要中间指针常量。你应该能够做到:
result = AudioUnitSetProperty(
samplerAudioUnit,
UInt32(kAUSamplerProperty_LoadPresetFromBank),
UInt32(kAudioUnitScope_Global),
0,
&bpData,
8)
func有以下定义:
func AudioUnitSetProperty(
inUnit: AudioUnit,
inID: AudioUnitPropertyID,
inScope: AudioUnitScope,
inElement: AudioUnitElement,
inData: ConstUnsafePointer<()>,
inDataSize: UInt32
) -> OSStatus
这一行:
inData: ConstUnsafePointer<()>,
翻译自C:
const void * inData,
inData
参数是指向任何内容的常量指针,这就是ConstUnsafePointer<()>
的含义。我无法在核心音频应用程序中尝试这个确切的代码,但我已经得到了类似的代码来工作。关于swift - 如何在Swift中创建ConstUnsafePointer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24685915/