修复 Inflate 中的 UnsafePointer 警告

问题描述

我仍然使用很棒的 WebSocketSwift https://github.com/tidwall/SwiftWebSocket 因为这个习惯用法比现在 iOS 中的 websockets 要好得多,

strm.next_in = UnsafePointer<UInt8>(inflateEnd)

结果

Initialization of 'UnsafePointer<UInt8>' results in a dangling pointer

Implicit argument conversion from '[UInt8]' to 'UnsafePointer<UInt8>' produces a
pointer valid only for the duration of the call to 'init(_:)'
Use the 'withUnsafeBufferPointer' method on Array in order to explicitly convert
argument to buffer pointer valid for a defined scope

:-(

有没有简单的方法可以消除警告?

https://github.com/tidwall/SwiftWebSocket/blob/master/Source/WebSocket.swift

func inflate(_ bufin : UnsafePointer<UInt8>,length : Int,final : Bool)
                       throws -> (p : UnsafeMutablePointer<UInt8>,n : Int){
    var buf = buffer
    var bufsiz = bufferSize
    var buflen = 0
    for i in 0 ..< 2{
        if i == 0 {
            strm.avail_in = CUnsignedInt(length)
            strm.next_in = UnsafePointer<UInt8>(bufin)
        } else {
            if !final {
                break
            }
            strm.avail_in = CUnsignedInt(inflateEnd.count)
            strm.next_in = UnsafePointer<UInt8>(inflateEnd)
        }
        while true {
            strm.avail_out = CUnsignedInt(bufsiz)
            strm.next_out = buf?.assumingMemoryBound(to: UInt8.self)
            _ = inflateG(&strm,flush: 0)
            let have = bufsiz - Int(strm.avail_out)
            bufsiz -= have
            buflen += have
            if strm.avail_out != 0{
                break
            }
            if bufsiz == 0 {
                bufferSize *= 2
                let nbuf = realloc(buffer,bufferSize)
                if nbuf == nil {
                    throw WebSocketError.payloadError("memory")
                }
                buffer = nbuf
                buf = buffer?.advanced(by: Int(buflen))
                bufsiz = bufferSize - buflen
            }
        }
    }
    return (buffer!.assumingMemoryBound(to: UInt8.self),buflen)
}

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)