Declaring self-referencing pointers in Swift [duplicate]

╄→гoц情女王★ 提交于 2019-12-19 09:18:21

问题


What would be the equivalent in swift of this Obj-C code?

I'm guessing something with CMutableVoidPointer

static void *CapturingStillImageContext = &CapturingStillImageContext;

回答1:


Probably something like this:

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



回答2:


CMutableVoidPointer isn't available anymore as of Beta 3. Theres UnsafePointer and ConstUnsafePointer - you can declare it this way:

let CapturingStillImageContext = UnsafePointer<()>()



回答3:


I've been using this technique, but it would be nice to find a more elegant solution:

var SelfReferencingKey = initialize_SelfReferencingKey()

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


来源:https://stackoverflow.com/questions/24628304/declaring-self-referencing-pointers-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!