无法为“UnsafeMutablePointer"类型调用初始化程序 [英] Cannot invoke initializer for type 'UnsafeMutablePointer'

查看:38
本文介绍了无法为“UnsafeMutablePointer"类型调用初始化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Reachability.swift 更新到 swift 3.0,但无法将 Reachability 实例传递给回调函数.

I'm trying to update Reachability.swift to swift 3.0 and I'm having trouble passing the Reachability instance to the call back function.

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

Here is my snippet: * please note self = Reachability class

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

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

编译器抛出错误的地方说:

Where the compiler throws an error saying:

无法调用类型为UnsafeMutablePointer<_>"的初始化程序'(UnsafeMutableRawPointer)' 类型的参数列表

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

指针转换受限:使用'.assumingMemoryBound(to:)'或'.bindMemory(to:capacity:)' 将内存视为一种类型.

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

UnsafeMutablePointer<_>"的重载与这些部分存在匹配参数列表:(RawPointer)、(OpaquePointer)、(OpaquePointer?), (UnsafeMutablePointer),(UnsafeMutablePointer?)

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

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

What I understand I need to convert self which is of type open class Reachability: NSObject to an UnsafeMutablPointer but Im not sure how to proceed.

推荐答案

检查 info 属性的类型来自 最新参考:

Check the type of info property from the latest reference:

声明

var info: UnsafeMutableRawPointer?

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

And the type of toOpaque() has become UnsafeMutableRawPointer. (I couldn't have found an up-to-date Apple document, but you can check it easily in the Quick Help pane of Xcode.)

您无需转换:

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

这篇关于无法为“UnsafeMutablePointer"类型调用初始化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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