我有一个技巧可以帮助测试UIAlertController
在 Swift 2.x 中有效:
extension UIAlertController {
typealias AlertHandler = @convention(block) (UIAlertAction) -> Void
func tapButtonAtIndex(index: Int) {
let block = actions[index].valueForKey("handler")
let handler = unsafeBitCast(block, AlertHandler.self)
handler(actions[index])
}
}
这在 Swift 3.x with 下失败了fatal error: can't unsafeBitCast between types of different sizes
,这让我相信可能有一种方法可以使演员表工作。任何人都可以弄清楚吗?