1

我正在尝试将 OSC 库从 swift 2 转换为 swift 3。我只剩下两个无法解决的错误。错误在下面的代码中。

错误由下式给出UnsafePointer

错误1:

无法将“sockaddr”类型的值转换为预期的参数类型“UnsafePointer!”

错误2:

'init' 不可用:使用 'withMemoryRebound(to:capacity:_)' 临时将内存视为另一种布局兼容类型。

如何修复这两个错误并将它们转换为 swift 3?

if (sa_family_ == UInt8(AF_INET)) {
            withUnsafePointer(to: &addr4_) { ptr -> Void in
                //let addrptr = UnsafePointer<sockaddr>(ptr)
                let addrptr = UnsafePointer(ptr).withMemoryRebound(to: sockaddr.self, capacity: 1) {
                    $0.pointee
                }
                sendto(sock_, sendbuf_, sendbuf_.count, 0,
                    addrptr, socklen_t(MemoryLayout<sockaddr_in>.size)) //Cannot convert value of type 'sockaddr' to expected argument type 'UnsafePointer<sockaddr>!'
            }
        } else {
            withUnsafePointer(to: &addr6_) { ptr -> Void in
                let addrptr = UnsafePointer<sockaddr>(ptr) // 'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.
                sendto(sock_, sendbuf_, sendbuf_.count, 0,
                    addrptr, socklen_t(MemoryLayout<sockaddr_in6>.size))
            }
        }
4

0 回答 0