I had a trick to help test UIAlertController
that worked in Swift 2.x:
extension UIAlertController {
typealias AlertHandler = @convention(b
Found a solution that works in Swift 3.0.1
extension UIAlertController {
typealias AlertHandler = @convention(block) (UIAlertAction) -> Void
func tapButton(atIndex index: Int) {
if let block = actions[index].value(forKey: "handler") {
let blockPtr = UnsafeRawPointer(Unmanaged.passUnretained(block as AnyObject).toOpaque())
let handler = unsafeBitCast(blockPtr, to: AlertHandler.self)
handler(actions[index])
}
}
}
(Originally, the block
value was the actual block, not a pointer to the block—which you obviously can't cast to a pointer to AlertHandler
)