Skip to content

Instantly share code, notes, and snippets.

@berikv
Created August 20, 2025 13:55
Show Gist options
  • Select an option

  • Save berikv/310b493f837a93b693af7c1bef2969d3 to your computer and use it in GitHub Desktop.

Select an option

Save berikv/310b493f837a93b693af7c1bef2969d3 to your computer and use it in GitHub Desktop.
Print the type signature of an objc method found in the runtime
import ObjectiveC.runtime
private func dumpMethodSignature(_ clsName: String, _ selName: String) {
let cls: AnyClass = NSClassFromString(clsName)!
let sel = NSSelectorFromString(selName)
let m = class_getInstanceMethod(cls, sel)!
let nargs = method_getNumberOfArguments(m)
let ret = String(cString: method_copyReturnType(m))
let enc = method_getTypeEncoding(m).map { String(cString: $0) } ?? "?"
print("== \(clsName) \(selName)")
print(" encoding: \(enc)")
print(" return: \(ret)")
print(" args (\(nargs)):")
for i in 0..<nargs {
if let t = method_copyArgumentType(m, i) {
print(" [\(i)] \(String(cString: t))")
free(t)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment