无法调用类型 'UnsafeMutablePointer' 的初始值设定项

Cannot invoke initializer for type 'UnsafeMutablePointer'

我正在尝试将 Reachability.swift 更新为 swift 3.0,但我在将 Reachability 实例传递给回调函数时遇到了问题。

这是我的代码片段: * 请注意 self = Reachability class

var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)

context.info = UnsafeMutablePointer(Unmanaged.passUnretained(self).toOpaque())

编译器抛出错误说:

Cannot invoke initializer for type 'UnsafeMutablePointer<_>' with an argument list of type '(UnsafeMutableRawPointer)'

Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type.

Overloads for 'UnsafeMutablePointer<_>' exist with these partially matching parameter lists: (RawPointer), (OpaquePointer), (OpaquePointer?), (UnsafeMutablePointer), (UnsafeMutablePointer?)

据我所知,我需要将 open class Reachability: NSObject 类型的 self 转换为 UnsafeMutablPointer,但我不确定如何进行。

检查 info 属性 来自 the latest reference 的类型:

Declaration

var info: UnsafeMutableRawPointer?

并且 toOpaque() 的类型变成了 UnsafeMutableRawPointer。 (我找不到最新的 Apple 文档,但您可以在 Xcode 的“快速帮助”窗格中轻松查看它。)

您无需转换:

    context.info = Unmanaged.passUnretained(self).toOpaque()