无法使用 (UnsafeMutableRawPointer) 类型的参数列表调用类型 UnsafePointer<_> 的初始值设定项

Cannot invoke initializer for type UnsafePointer<_> with an argument list of type (UnsafeMutableRawPointer)

当我从音频缓冲区数据写入多点连接中的输出流时,出现错误

Cannot invoke initializer for type UnsafePointer<_> with an argument list of type (UnsafeMutableRawPointer)

我找到了很多解决方案,但这个解决方案对我没有帮助。

我的代码是:

func send(_ buffer: AudioBuffer) {
        print(buffer.mData!)
        print(buffer.mDataByteSize)

        outputStreme?.write(UnsafePointer(buffer.mData), maxLength: buffer.mDataByteSize)
    }

提前致谢..:)

当某些规范更改影响了您的代码时,请查看官方参考资料。在你的情况下 AudioBuffer.mData 是类型 UnsafeMutableRawPointer?,你需要将它传递给类型 UnsafePointer<UInt8>.

OutputStream.write(_:maxLength:) 的第一个参数

UnsafeMutableRawPointer

你可以找到这个方法 returns UnsafeMutablePointer<T>:

func assumingMemoryBound<T>(to: T.Type)

bound 的概念有点混乱,但似乎你可以用它来进行指针类型转换:

outputStreme?.write(buffer.mData!.assumingMemoryBound(to: UInt8.self), maxLength: Int(buffer.mDataByteSize))

(假设强制解包 ! 按照您的 print(buffer.mData!) 的建议足够安全。)

Memory bound-ness 对于大多数 return 指针的 API 没有很好的定义,并且目前没有效果。还有另一种类型转换方法 func bindMemory<T>(to: T.Type, capacity: Int),两者都没有问题(同样,就目前而言)。

试试这个:

withUnsafePointer(to: &data) {rawUuidPtr in //<- `rawUuidPtr` is of type `UnsafePointer<uuid_t>`.
            let bytes = UnsafeRawPointer(rawUuidPtr).assumingMemoryBound(to: UInt8.self)
            outputStream.write(bytes, maxLength: 4)
        }