Swift功能swizzling 运行时

Swift功能swizzling 运行时,第1张

概述在Swift之前,在Objective-C中,我将使用< objc / runtime.h>在一个类中进行swizzle或hook方法。 如果任何人有任何关于修改Swift的运行时和挂起功能(如CydiaSubstrate和其他帮助此领域的库)的信息,请通知我。 我在Swift中用方法swizzling成功。这个例子展示了如何在NSDictionary上挂钩描述方法 我的实现: extension @H_419_0@ 在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

总结

以上是内存溢出为你收集整理的Swift功能swizzling /运行时全部内容,希望文章能够帮你解决Swift功能swizzling /运行时所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1053570.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存