我正在嘗試檢查用戶是否具有Internet連接,並且該過程的一部分涉及調用withUnsafePointer
。在斯威夫特1.x中,我可以使用:withUnsafePointer in Swift 2
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()
}
但是現在有了斯威夫特2,我得到一個錯誤說
不能援引「withUnsafePointer」類型的參數列表「 (INOUT SOCKADDR_IN,(_) - > _)」
我看着Xcode的使用,這就是:
所以我試圖用
withUnsafePointer(&zeroAddress) {(pointer: UnsafePointer<sockaddr_in>) -> sockaddr_in in
SCNetworkReachabilityCreateWithAddress(nil, pointer).takeRetainedValue()
}
以及
withUnsafePointer(&zeroAddress) {(pointer: UnsafePointer<sockaddr_in>) -> AnyObject in
SCNetworkReachabilityCreateWithAddress(nil, pointer).takeRetainedValue()
}
,他們都給予cannot invoke...
編譯時錯誤。在Swift 2.x中使用withUnsafePointer
的正確方法是什麼?
所以現在好多了.. – Berik