如何在Swift 3中将__NSMallocBlock__转换为其基础类型? [英] How do I cast an __NSMallocBlock__ to its underlying type in Swift 3?

查看:298
本文介绍了如何在Swift 3中将__NSMallocBlock__转换为其基础类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个技巧,可以帮助测试在Swift 2.x中工作的UIAlertController:

I had a trick to help test UIAlertController that worked in 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])
    }

}

在带有fatal error: can't unsafeBitCast between types of different sizes的Swift 3.x下,此操作失败,这使我相信可能有一种方法可以进行强制转换.有人能弄清楚吗?

This fails under Swift 3.x with fatal error: can't unsafeBitCast between types of different sizes, which tempts me to believe there might be a way to make the cast work. Can anyone figure it out?

推荐答案

找到了可在Swift 3.0.1中使用的解决方案

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<AnyObject>.passUnretained(block as AnyObject).toOpaque())
            let handler = unsafeBitCast(blockPtr, to: AlertHandler.self)
            handler(actions[index])
        }
    }

}

(最初,block值是实际的块,而不是指向该块的指针-您显然不能将其转换为指向AlertHandler的指针)

(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)

这篇关于如何在Swift 3中将__NSMallocBlock__转换为其基础类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆