我的代码片段如下......:
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}
...不再编译并出现以下我不明白的错误:
"'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type."
如何解决这个问题?
最佳答案
来自 Xcode 8 beta 6 的发行说明:
- An
Unsafe[Mutable]RawPointer
type has been introduced, replacingUnsafe[Mutable]Pointer<Void>
. Conversion fromUnsafePointer<T>
toUnsafePointer<U>
has been disallowed.Unsafe[Mutable]RawPointer
provides an API for untyped memory access, and an API for binding memory to a type. Binding memory allows for safe conversion between pointer types. SeebindMemory(to:capacity:)
,assumingMemoryBound(to:)
, andwithMemoryRebound(to:capacity:)
. (SE-0107)
根据您的情况,您可能需要编写如下内容:
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}
关于swift3 - Swift 3 UnsafePointer($0) 不再在 Xcode 8 beta 6 中编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39046377/