withUnsafePointer in Swift 2

后端 未结 3 1146
挽巷
挽巷 2020-12-28 17:41

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:

3条回答
  •  北海茫月
    2020-12-28 18:10

    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)
        }
    })
    

提交回复
热议问题