Cannot invoke initializer for type 'UnsafeMutablePointer'

无人久伴 提交于 2019-12-22 10:09:08

问题


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.

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:

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

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.


回答1:


Check the type of info property from the latest reference:

Declaration

var info: UnsafeMutableRawPointer?

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

You have no need to convert:

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


来源:https://stackoverflow.com/questions/39259896/cannot-invoke-initializer-for-type-unsafemutablepointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!