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

标签 swift3 unsafe-pointers xcode8-beta6

我的代码片段如下......:

    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)
    }
}

关于swift3 - Swift 3 UnsafePointer($0) 不再在 Xcode 8 beta 6 中编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39046377/

相关文章:

ios - 在 UITextField swift 3.0 中使用数字键盘以 dd-MM-yyyy 格式输入日期

ios - 根据数据更新rootview Controller

ios - Swift 3.0 中的 Objective C 代码

c - 在 swift 5 中将 void* 参数传递给 c 函数时,'withUnsafeBytes' 被弃用警告

ios - 如何将数组转换为 UnsafeMutablePointer<UnsafeRawPointer?> Swift 3.0?

go - golang从dll返回char *作为返回值

ios - 使用 Xcode 8 和 watchOS3 配置 WatchKit 应用程序扩展

xcode - swift : The smallest excutable for-loop

ios - HealthKit - 请求授权(toShare :read:completion:) always succeeds

ios-simulator - Xcode 8 模拟器 ios 8 图像失真