withUnsafePointer in Swift 2
Asked Answered
M

3

17

I'm trying to check if the user has internet connection, and part of the process involves invoking withUnsafePointer. In Swift 1.x, I was able to use:

var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)

let defaultRouteReachability = withUnsafePointer(&zeroAddress){
    SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)).takeRetainedValue()
}

But now with Swift 2, I'm getting an error saying

Cannot invoke 'withUnsafePointer' with an argument list of type '(inout sockaddr_in, (_) -> _)'

I looked Xcode's usage, which is:

enter image description here

So I tried using

withUnsafePointer(&zeroAddress) {(pointer: UnsafePointer<sockaddr_in>) -> sockaddr_in in
    SCNetworkReachabilityCreateWithAddress(nil, pointer).takeRetainedValue()
}

as well as

withUnsafePointer(&zeroAddress) {(pointer: UnsafePointer<sockaddr_in>) -> AnyObject in
    SCNetworkReachabilityCreateWithAddress(nil, pointer).takeRetainedValue()
}

And they both give a cannot invoke... compile-time error. What is the correct way to use withUnsafePointer in Swift 2.x?

Misquote answered 9/6, 2015 at 18:28 Comment(0)
T
23

The error message is misleading, the problem is that SCNetworkReachabilityCreateWithAddress() does not return an unmanaged object anymore, so you must not call takeRetainedValue():

var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)

let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
    SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}

Note also the simplified creation of C structs like struct sockaddr_in which was introduced with Swift 1.2 (if I remember correctly).

Tarkington answered 9/6, 2015 at 18:34 Comment(0)
A
2

Swift 3:

var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)

let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {
    _ = $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { zeroSockAddress in
        SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
    }
})
Ameeameer answered 20/9, 2016 at 11:4 Comment(1)
hi Lio...in the line --> SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress) i am getting the following error.--> use of unsolved identifier 'SCNetworkReachabilityCreateWithAddress'Bedrail
D
0

Swift3 use this

         var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
         zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
         zeroAddress.sin_family = sa_family_t(AF_INET)

         let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
            $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
                SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
            }
        }

        var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
        if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
            return false
        }
Desiccate answered 15/11, 2016 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.