简体   繁体   English

如何将我的swift类转换为与objective-c中的__bridge类似的UnsafePointer

[英]How do I convert my swift class to an UnsafePointer like the __bridge in objective-c

I'm trying to implement a swift bridge to the SecureTransport C libraries. 我正在尝试实现SecureTransport C库的快速桥接。 I 'think' I can pass in anything as the connection as long as I know how to read stuff from it in my sslReadCallback/sslWriteCallback implementations. 我认为'只要我知道如何在我的sslReadCallback / sslWriteCallback实现中读取内容,我就可以传递任何连接作为连接。 This is the assumption I'm working with. 这是我正在使用的假设。 This seems to be the case when I view the definition of SSLConnectionRef: 当我查看SSLConnectionRef的定义时,似乎就是这种情况:

/* Opaque reference to an I/O connection (socket, endpoint, etc.) */
public typealias SSLConnectionRef = UnsafePointer<Void>

So I just need to turn my class into UnsafePointer. 所以我只需要把我的班级变成UnsafePointer。 Unfortunately the compiler doesn't like my attempts. 不幸的是编译器不喜欢我的尝试。 Can any one give me tips? 任何人都可以给我提示吗?

func startSSLProcess()
{
    self.sslContext = SSLCreateContext(kCFAllocatorDefault, SSLProtocolSide.ClientSide, SSLConnectionType.StreamType)
    if let sslContext = self.sslContext
    {
        SSLSetIOFuncs(sslContext, sslReadCallback, sslWriteCallback)
        SSLSetConnection(sslContext, UnsafePointer(self)) // <-- error
        SSLSetSessionOption(sslContext, SSLSessionOption.BreakOnClientAuth, true)
        SSLHandshake(sslContext)
    }
}

In GCDAsyncSocket, it does this: 在GCDAsyncSocket中,它执行此操作:

status = SSLSetConnection(sslContext, (__bridge SSLConnectionRef)self);

and

SubZeroGCDAsyncSocket *asyncSocket = (__bridge SubZeroGCDAsyncSocket *)connection;

.. to unwrap. ..打开包装。 Whats the swift equivalent of this? 什么是快速相当于这个?

Thanks very much! 非常感谢!

The answer was here: 答案在这里:

How to cast self to UnsafeMutablePointer<Void> type in swift 如何在Swift中将self转换为UnsafeMutablePointer <Void>类型

So now I do the following: 所以现在我做以下事情:

func startSSLProcess()
{
    self.sslContext = SSLCreateContext(kCFAllocatorDefault, SSLProtocolSide.ClientSide, SSLConnectionType.StreamType)
    if let sslContext = self.sslContext
    {
        SSLSetIOFuncs(sslContext, sslReadCallback, sslWriteCallback)
        SSLSetConnection(sslContext, UnsafePointer(Unmanaged.passUnretained(self).toOpaque()))
        SSLSetSessionOption(sslContext, SSLSessionOption.BreakOnClientAuth, true)
        SSLHandshake(sslContext)
    }
}

And I unwrap like this: 我打开这样的东西:

    let transportWrapper:SecureTransportWrapper = Unmanaged<SecureTransportWrapper>.fromOpaque(COpaquePointer(connection)).takeUnretainedValue()

Substitute your own type for SecureTransportWrapper. 用您自己的类型替换SecureTransportWrapper。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM