Swift 3 UnsafePointer($ 0)不再在Xcode 8 beta 6中编译 [英] Swift 3 UnsafePointer($0) no longer compile in Xcode 8 beta 6

查看:71
本文介绍了Swift 3 UnsafePointer($ 0)不再在Xcode 8 beta 6中编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码片段如下……:

My code snipet as follows …:

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

…不再编译有我不明白的以下错误:

… does no longer compile with the following error which I don't understand:

"'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type."

该如何解决?

推荐答案

摘自Xcode 8 beta 6发行说明:

From the Release Notes of Xcode 8 beta 6:

  • 引入了Unsafe[Mutable]RawPointer类型,代替了Unsafe[Mutable]Pointer<Void>.从UnsafePointer<T>转换为 UnsafePointer<U>已被禁止. Unsafe[Mutable]RawPointer 提供用于无类型内存访问的API和用于绑定的API 记忆类型.绑定内存可确保在 指针类型.参见bindMemory(to:capacity:)assumingMemoryBound(to:), 和withMemoryRebound(to:capacity:). (SE-0107)
  • 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)

在您的情况下,您可能需要编写如下内容:

In your case, you may need to write something like this:

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

这篇关于Swift 3 UnsafePointer($ 0)不再在Xcode 8 beta 6中编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆