SNos
SNos

Reputation: 3470

Swift 3 - Migrating error Cannot convert value of type 'sockaddr'

I am trying to convert a OSC library from swift 2 to swift 3. I have only two errors left that I caanot solve. The errors are in the code below.

The errors are given by UnsafePointer

error 1:

Cannot convert value of type 'sockaddr' to expected argument type 'UnsafePointer!'

error 2:

'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.

How Could I fix these two errors and convert them for 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))
            }
        }

Upvotes: 1

Views: 846

Answers (0)

Related Questions