加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 06:09:07 所属栏目:百科 来源:网络整理
导读:我的代码snipet如下…: let defaultRouteReachability = withUnsafePointer(to: zeroAddress) { SCNetworkReachabilityCreateWithAddress(nil,UnsafePointer($0)) } …不再编译与以下错误,我不明白: "'init' is unavailable: use 'withMemoryRebound(to:c
我的代码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)
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读