从Swift 2开始,接受C回调的C函数可以在没有中间包装器的情况下从Swift调用。
许多C事件处理API都遵循先创建上下文的模式:
struct Context {
CFIndex version;
void *info;
CFAllocatorRetainCallBack retain;
CFAllocatorReleaseCallBack release;
CFAllocatorCopyDescriptionCallBack copyDescription;
}然后在注册回调时将上下文传递给C API。当接收到事件时,info指针随事件一起传递给回调函数,允许我们恢复上下文状态(因为C函数不允许捕获状态)。
我希望正确地创建这样的上下文,包括retain、release和copyDescription回调。
对于copyDescription,签名是
typealias CFAllocatorCopyDescriptionCallBack =
(UnsafePointer<Void>) -> Unmanaged<CFString>!如何从Swift创建Unmanaged<CFString>!实例,即如何将Swift string对象从ARC内存模型转移到外部托管模型?
发布于 2015-12-05 01:34:13
请参阅问题注释。
let string = Unmanaged.passRetained("description")https://stackoverflow.com/questions/34093005
复制相似问题