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

时间:2017-07-19 05:26:09

标签: ios xcode swift3 outputstream multipeer-connectivity

当我从音频缓冲区数据写入多路连接的输出流时,我得到了错误

  

无法为类型UnsafePointer< _>调用初始值设定项有一个参数   类型列表(UnsafeMutableRawPointer)

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

我的代码是:

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

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

enter image description here

提前致谢.. :))

2 个答案:

答案 0 :(得分:3)

当某些规格更改影响您的代码时,请检查官方参考。在您的情况下,AudioBuffer.mData的类型为UnsafeMutableRawPointer?,您需要将其传递给OutputStream.write(_:maxLength:)类型UnsafePointer<UInt8>的第一个参数。

UnsafeMutableRawPointer

你可以找到这个返回UnsafeMutablePointer<T>的方法:

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

bound 的概念有点令人困惑,但似乎你可以将它用于指针类型转换:

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

(假设强制解包!足够安全,正如print(buffer.mData!)所建议的那样。)

对于大多数返回指针的API,

内存绑定 -ness没有很好地定义,并且现在没有效果。还有另一种类型转换方法func bindMemory<T>(to: T.Type, capacity: Int),两者都没有问题(同样,现在也是如此)。

答案 1 :(得分:1)

试试这个:

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)
        }