美文网首页
如何在Swift中使用Aspects

如何在Swift中使用Aspects

作者: championfu | 来源:发表于2019-08-27 17:20 被阅读0次

Aspects是一个轻量级的优秀AOP的工具,Aspects是用OC编写,OC强大的运行时特性,使得类型转换十分方便,但这一点在Swift中存在一点问题,利用Swift的编译器可以将Block对象转换成Swift的闭包.话不多说,下面的分类请点个赞后拿走

extension NSObject {
  
    typealias AspectsBlock = (AspectInfo)->()
    
    func swift_hook(with sel: Selector, options: AspectOptions, usingBlock: @escaping AspectsBlock) throws {
        let blockObj: @convention(block) (_ id: AspectInfo)->Void = { aspectInfo in
            usingBlock(aspectInfo)
        }
        try self.aspect_hook(sel, with: options, usingBlock: blockObj)
    }
    
    static func swift_hook(with sel: Selector, options: AspectOptions, usingBlock: @escaping AspectsBlock) throws {
        let blockObj: @convention(block) (_ id: AspectInfo)->Void = { aspectInfo in
            usingBlock(aspectInfo)
        }
        try self.aspect_hook(sel, with: options, usingBlock: blockObj)
    }
}

相关文章

网友评论

      本文标题:如何在Swift中使用Aspects

      本文链接:https://www.haomeiwen.com/subject/tgtyectx.html