swift3 – Swift 3 UnsafePointer($0)不再在Xcode 8 beta 6中编译

前端之家收集整理的这篇文章主要介绍了swift3 – Swift 3 UnsafePointer($0)不再在Xcode 8 beta 6中编译前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的代码snipet如下…:
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,replacing Unsafe[Mutable]Pointer<Void>. Conversion from UnsafePointer<T> to
    UnsafePointer<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. See bindMemory(to:capacity:),assumingMemoryBound(to:),
    and withMemoryRebound(to:capacity:). (SE-0107)

在你的情况下,你可能需要这样写:

let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
    $0.withMemoryRebound(to: sockaddr.self,capacity: 1) {zeroSockAddress in
        SCNetworkReachabilityCreateWithAddress(nil,zeroSockAddress)
    }
}
原文链接:https://www.f2er.com/swift/321295.html

猜你在找的Swift相关文章