问题描述
当我从音频缓冲区数据以多点连接方式写入输出流时,出现错误
When I write the output stream in multipeer connectivity from audio buffer data I got the error
无法使用参数调用 UnsafePointer<_> 类型的初始化程序类型列表(UnsafeMutableRawPointer)
我找到了很多解决方案,但这个解决方案对我没有帮助.
I found the many solutions, but this solution not helpful for me.
我的代码是:
func send(_ buffer: AudioBuffer) {
print(buffer.mData!)
print(buffer.mDataByteSize)
outputStreme?.write(UnsafePointer(buffer.mData), maxLength: buffer.mDataByteSize)
}
提前致谢..:)
推荐答案
当某种规范更改影响到您的代码时,请查看官方参考.在您的情况下,AudioBuffer.mData
属于 UnsafeMutableRawPointer?
类型,您需要将其传递给 OutputStream.write(_:maxLength:) 类型为
UnsafePointer
.
Please check the official reference when some sort of spec changes has affected with your code. In your case
AudioBuffer.mData
is of type UnsafeMutableRawPointer?
, and you need to pass it to the first argument of OutputStream.write(_:maxLength:)
of type UnsafePointer<UInt8>
.
你可以找到这个返回
UnsafeMutablePointer
的方法:
You can find this method which returns
UnsafeMutablePointer<T>
:
func assumingMemoryBound(to: T.Type)
bound 的概念有点令人困惑,但您似乎可以将其用于指针类型转换:
The concept of bound is sort of confusing, but seems you can use it for pointer type conversion:
outputStreme?.write(buffer.mData!.assumingMemoryBound(to: UInt8.self), maxLength: Int(buffer.mDataByteSize))
(假设强制解包
!
按照您的 print(buffer.mData!)
的建议足够安全.)
(Assuming forced-unwrapping
!
is safe enough as suggested by your print(buffer.mData!)
.)
内存绑定-ness 没有很好地定义,并且目前没有影响.还有另一种类型转换方法
func bindMemory(to: T.类型,容量:Int)
,两者都可以正常工作(再次,就目前而言).
Memory bound-ness is not well defined for most APIs which return pointers, and has no effect as for now. There's another type conversion method
func bindMemory<T>(to: T.Type, capacity: Int)
, and both work without problems (again, as for now).
这篇关于无法为类型 UnsafePointer<_> 调用初始化程序带有类型的参数列表 (UnsafeMutableRawPointer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!