I had a trick帮助测试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 下失败了fatal error: can't unsafeBitCast between types of different sizes
,这让我相信可能有办法让演员阵容发挥作用。有人能弄清楚吗?
找到了适用于 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<AnyObject>.passUnretained(block as AnyObject).toOpaque())
let handler = unsafeBitCast(blockPtr, to: AlertHandler.self)
handler(actions[index])
}
}
}
(原本,block
value 是实际的块,而不是指向块的指针——显然你不能将其转换为指向的指针AlertHandler
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)