在Swift之前,在Objective-C中,我会使用来混淆或钩住类中的方法<objc/runtime.h>
。
如果有人对修改Swift的运行时以及钩子函数(如CydiaSubstrate)和其他在此方面提供帮助的库有任何信息,请通知我。
我已经在Swift中成功地使用了方法。本示例说明如何在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类(但不适用于不自定义的Swift类。)更多示例-https: //github.com/mbazaliy/MBSwizzler
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句