如果任何人有任何关于修改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 /运行时所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)