Created
November 6, 2018 11:35
-
-
Save hossamghareeb/a2a641947c41b37e12333e7596dab2b9 to your computer and use it in GitHub Desktop.
Swizzle and print all method of a class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func swizzle(method: String, of class: String, to newMethod: String) { | |
let original: Method | |
let swizzled: Method | |
guard let aClass = objc_getClass(`class`) as? AnyClass else { return } | |
original = class_getInstanceMethod(aClass, Selector((method)))! | |
swizzled = class_getInstanceMethod(aClass, Selector((newMethod)))! | |
method_exchangeImplementations(original, swizzled) | |
} | |
private func printAllMethodsForClass(_ class: AnyClass) { | |
var methodCount: UInt32 = 0 | |
let methodList = class_copyMethodList(`class`, &methodCount) | |
for i in 0..<Int(methodCount) { | |
let unwrapped = methodList?[i] | |
print(NSStringFromSelector(method_getName(unwrapped!))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment