Skip to content

Instantly share code, notes, and snippets.

@hossamghareeb
Created November 6, 2018 11:35
Show Gist options
  • Save hossamghareeb/a2a641947c41b37e12333e7596dab2b9 to your computer and use it in GitHub Desktop.
Save hossamghareeb/a2a641947c41b37e12333e7596dab2b9 to your computer and use it in GitHub Desktop.
Swizzle and print all method of a class
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