The Swift Unmanaged class and its toOpaque and fromOpaque methods can be used to convert object references to pointers and back. Since “everything can be converted to AnyObject” in Swift, the following should work:
typealias Closure = () -> Void
let cl1: Closure = { print("Hello") }
// Closure to pointer:
let ptr = Unmanaged.passRetained(cl1 as AnyObject).toOpaque()
// Pointer to closure:
if let cl2 = Unmanaged<AnyObject>.fromOpaque(ptr).takeRetainedValue() as? Closure {
cl2()
}
As in your Objective-C version, you are responsible for properly balancing the passRetained and takeRetainedValue calls.
There is also passUnretained and takeUnretainedValue which correspond to the __bridge conversion in Objective-C and do not retain or release the object.
Here ptr is an UnsafeMutableRawPointer which corresponds to void * in C. If you need an integer type then you can convert the pointer to an UInt and back with
let v = UInt(bitPattern: ptr)
let p = UnsafeRawPointer(bitPattern: v)