将 NSData 复制到 UnsafeMutablePointer<Void>
copy NSData to UnsafeMutablePointer<Void>
你好Whosebugers。我正在为安全传输实现一个包装器,但我坚持使用某些 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
}
}
编译错误的位置我已经标出来了。我不怪它犯错,我在这里有点猜测:P。我正在尝试将 NSData 复制到 data:UnsafeMutablePointer。我该怎么做?
编译错误:
/Users/*/SecureTransportWrapper.swift:108:9: Static member 'alloc' cannot be used on instance of type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<()>')
非常感谢!
================
更新:这是关于 sslReadCallback 应该做什么的 api 文档:
connection: A connection reference.
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. On return, your callback
should overwrite that integer with the number of bytes actually
transferred.
摘自here
好的,让我们看一下您的代码:
dataLength = UnsafeMutablePointer<Int>.alloc(1)
dataLength.initialize(bytesRead.length)
dataLength
是你传入的指针,它是函数的调用者给你缓冲区大小的地方 和 希望你把您读取的字节数。你不需要分配这个,它已经分配了。
(与此示例无关,但:同样在 alloc(N) 和 initialize(N) 中,N 应该相同(它是分配的内存量,然后初始化))
我想你想要的(Swift 3 使用 pointee
而不是 memory
)是这样的:
dataLength.memory = bytesRead.length
C API 表示您还可以从该变量中获得 data
缓冲区的大小。 data
将为此大小预先分配。
确保你读取的数据适合(bytesRead.length <= dataLength.memory
),然后做一个
memcpy(data, bytesRead.bytes, bytesRead.length)
就这些了。
你好Whosebugers。我正在为安全传输实现一个包装器,但我坚持使用某些 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
}
}
编译错误的位置我已经标出来了。我不怪它犯错,我在这里有点猜测:P。我正在尝试将 NSData 复制到 data:UnsafeMutablePointer。我该怎么做?
编译错误:
/Users/*/SecureTransportWrapper.swift:108:9: Static member 'alloc' cannot be used on instance of type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<()>')
非常感谢!
================
更新:这是关于 sslReadCallback 应该做什么的 api 文档:
connection: A connection reference.
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. On return, your callback should overwrite that integer with the number of bytes actually transferred.
摘自here
好的,让我们看一下您的代码:
dataLength = UnsafeMutablePointer<Int>.alloc(1)
dataLength.initialize(bytesRead.length)
dataLength
是你传入的指针,它是函数的调用者给你缓冲区大小的地方 和 希望你把您读取的字节数。你不需要分配这个,它已经分配了。
(与此示例无关,但:同样在 alloc(N) 和 initialize(N) 中,N 应该相同(它是分配的内存量,然后初始化))
我想你想要的(Swift 3 使用 pointee
而不是 memory
)是这样的:
dataLength.memory = bytesRead.length
C API 表示您还可以从该变量中获得 data
缓冲区的大小。 data
将为此大小预先分配。
确保你读取的数据适合(bytesRead.length <= dataLength.memory
),然后做一个
memcpy(data, bytesRead.bytes, bytesRead.length)
就这些了。