简体   繁体   English

将NSData复制到UnsafeMutablePointer <Void>

[英]copy NSData to UnsafeMutablePointer<Void>

Hi there stackoverflowers. 嗨,那里stackoverflowers。 I'm implementing a wrapper for Secure Transport and I'm stuck on some of the C -> Swift syntax. 我正在为安全传输实现一个包装器,并且停留在某些C-> Swift语法上。

func sslReadCallback(connection: SSLConnectionRef,
    data: UnsafeMutablePointer<Void>,
    var dataLength: UnsafeMutablePointer<Int>) -> OSStatus
{
    //let bytesRequested = dataLength.memory
    let transportWrapper:SecureTransportWrapper = UnsafePointer(connection).memory
    let bytesRead:NSData = transportWrapper.readFromConnectionFunc(transportWrapper.connection)

    dataLength = UnsafeMutablePointer<Int>.alloc(1)
    dataLength.initialize(bytesRead.length)

    if (bytesRead.length == 0)
    {
        return OSStatus(errSSLClosedGraceful)
    }
    else
    {
        data.alloc(sizeof(bytesRead.length)) //<----compile error here
        return noErr
    }
}

I've marked the location of the compile error. 我已经标记了编译错误的位置。 I don't blame it for erring, I was kind of guessing here :P. 我不怪它犯了错误,我在这里有点猜测:P。 I'm trying to copy the the NSData to the data:UnsafeMutablePointer. 我正在尝试将NSData复制到data:UnsafeMutablePointer。 How do I do that? 我怎么做?

Compile error: 编译错误:

/Users/*/SecureTransportWrapper.swift:108:9: Static member 'alloc' cannot be used on instance of type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<()>') /Users/*/SecureTransportWrapper.swift:108:9:静态成员'alloc'不能用于类型'UnsafeMutablePointer'(又名'UnsafeMutablePointer <()>')的实例

Thanks a ton! 万分感谢!

================ ================

Update: here is the api doc for what the sslReadCallback is supposed to do: 更新:这是sslReadCallback应该做什么的api文档:

connection: A connection reference. connection:连接参考。

data: On return, your callback should overwrite the memory at this location with the data read from the connection. 数据:返回时,回调应使用从连接读取的数据覆盖此位置的内存。

dataLength: On input, a pointer to an integer representing the length of the data in bytes. dataLength:输入时,指向整数的指针,该整数表示数据的长度(以字节为单位)。 On return, your callback should overwrite that integer with the number of bytes actually transferred. 返回时,回调应使用实际传输的字节数覆盖该整数。

Excerpt from here 这里摘录

OK, lets go through your code: 好的,让我们检查一下代码:

dataLength = UnsafeMutablePointer<Int>.alloc(1)
dataLength.initialize(bytesRead.length)

dataLength is a pointer you get passed in, it is where the caller of the function both gives you the size of the buffer and wants you to put the number of bytes you read. dataLength是您传入的指针,在此函数的调用者既可以提供缓冲区的大小, 可以输入读取的字节数。 You don't need to alloc this, it is already allocated. 您不需要分配它,它已经分配了。

(Irrelevant for this example but: Also in alloc(N) and initialize(N) the N should be the same (it is the amount of memory being allocated, and then initialized)) (此示例不重要,但:同样在alloc(N)和initialize(N)中,N应该相同(它是分配的内存量,然后进行初始化))

I think what you want (Swift 3 uses pointee instead of memory ) is this: 我认为您想要的(Swift 3使用pointee而不是memory )是这样的:

dataLength.memory = bytesRead.length

The C API says that you also get the size of the data buffer from this variable. C API指出,您还可以从此变量获取data缓冲区的大小。 data will be pre-allocated for this size. data将为此大小预先分配。

Make sure the data you read fits ( bytesRead.length <= dataLength.memory ), then just do a 确保您读取的数据适合( bytesRead.length <= dataLength.memory ),然后执行

memcpy(data, bytesRead.bytes, bytesRead.length)

That's all. 就这样。

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

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