在Core Audio
-Framework 中,用户数据可以通过UnsafeMutableRawPointer?
. 我想知道如何通过 this通过引用UnsafeMutableRawPointer?
传递结构。回调内部所做的更改应反映在回调外部。
我建立了一个游乐场来测试这个:
struct TestStruct {
var prop1: UInt32
var prop2: Float64
var prop3: Bool
}
func printTestStruct(prefix: String, data: TestStruct) {
print("\(prefix): prop1: \(data.prop1), prop2: \(data.prop2), prop3: \(data.prop3)")
}
func testUnsafeMutablePointer(data: UnsafeMutableRawPointer?) {
var testStructInFunc = data!.load(as: TestStruct.self)
printTestStruct(prefix: "In func (pre change)", data: testStructInFunc)
testStructInFunc.prop1 = 24
testStructInFunc.prop2 = 1.2
testStructInFunc.prop3 = false
printTestStruct(prefix: "In func (post change)", data: testStructInFunc)
}
var testStruct: TestStruct = TestStruct(prop1: 12, prop2: 2.4, prop3: true)
printTestStruct(prefix: "Before call", data: testStruct)
testUnsafeMutablePointer(data: &testStruct)
printTestStruct(prefix: "After call", data: testStruct)
可悲的是,在函数调用后,函数testStruct
内部所做的任何更改似乎都丢失了。testUnsafeMutablePointer
我在想,UnsafeMutableRawPointer
这里的行为就像通过引用传递?我错过了什么?