在Swift中声明自引用指针

时间:2014-07-08 09:28:02

标签: objective-c pointers swift self

这个Obj-C代码的快速等价是什么?

我猜错了CMutableVoidPointer

static void *CapturingStillImageContext = &CapturingStillImageContext;

3 个答案:

答案 0 :(得分:3)

从Beta 3开始,

CMutableVoidPointer不再可用.Theres UnsafePointerConstUnsafePointer - 您可以这样声明:

let CapturingStillImageContext = UnsafePointer<()>()

答案 1 :(得分:1)

可能是这样的:

var CapturingStillImageContext: COpaquePointer = nil
withUnsafePointer(&CapturingStillImageContext) {
    CapturingStillImageContext = COpaquePointer($0) }

答案 2 :(得分:0)

我一直在使用这种技术,但找到更优雅的解决方案会很好:

var SelfReferencingKey = initialize_SelfReferencingKey()

func initialize_SelfReferencingKey() -> UnsafePointer<()> {
    return withUnsafePointer(&SelfReferencingKey, { UnsafePointer($0) } )
}