加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift功能swizzling /运行时

发布时间:2020-12-14 05:53:43 所属栏目:百科 来源:网络整理
导读:在Swift之前,在Objective-C中,我将使用 objc / runtime.h在一个类中进行swizzle或hook方法。 如果任何人有任何关于修改Swift的运行时和挂起功能(如CydiaSubstrate和其他帮助此领域的库)的信息,请通知我。 我在Swift中用方法swizzling成功。这个例子展示了
在Swift之前,在Objective-C中,我将使用< objc / runtime.h>在一个类中进行swizzle或hook方法。

如果任何人有任何关于修改Swift的运行时和挂起功能(如CydiaSubstrate和其他帮助此领域的库)的信息,请通知我。

我在Swift中用方法swizzling成功。这个例子展示了如何在NSDictionary上挂钩描述方法

我的实现:

extension NSDictionary {
     func myDescription() -> String!{
        println("Description hooked")
        return "Hooooked " + myDescription();
    }
}

开关代码:

func swizzleEmAll() {
        var dict:NSDictionary = ["SuperSecret": kSecValueRef]
        var method: Method = class_getInstanceMethod(object_getClass(dict),Selector.convertFromStringLiteral("description"))

        println(dict.description) // Check original description

        var swizzledMethod: Method = class_getInstanceMethod(object_getClass(dict),Selector.convertFromStringLiteral("myDescription"))
        method_exchangeImplementations(method,swizzledMethod)

        println(dict.description) //Check that swizzling works
    }

编辑:
此代码将适用于从NSObject继承的任何自定义Swift类(但不适用于没有的类)。更多示例 – https://github.com/mbazaliy/MBSwizzler

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读