1

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))
            }
        }
SNos
  • 3,430
  • 5
  • 42
  • 92
  • This should be what you are looking for: https://stackoverflow.com/questions/39184544/cast-to-different-c-struct-unsafe-pointer-in-swift. – Martin R Jun 15 '17 at 21:41

0 Answers0