0

I can call implementation method, if I known his parameters. as example

let method: Method = class_getInstanceMethod(owner.dynamicType, selector)
let implementation = method_getImplementation(method)
// if i known this ((String, String) -> NSString)
typealias Function = @convention(c) (AnyObject, Selector, String, String) -> Unmanaged<NSString>
let function = unsafeBitCast(implementation, Function.self)
let result = { title, message in function(owner, selector, title, message).takeUnretainedValue() }

but what to do, if I dont known in previously type for method? I can do like something? maybe is possible tell arguments as array?

EvGeniy Ilyin
  • 1,817
  • 1
  • 21
  • 38
  • Take a look at NSMethodSignature (`methodSignatureForSelector:`) and NSInvocation. I'm not sure that Swift allows you to use these. – jtbandes Nov 28 '16 at 23:14
  • out of it be is difficult to build `typealias Function = @convention (с) (.....)`, I need generic way, if it exists – EvGeniy Ilyin Nov 28 '16 at 23:19
  • 1
    @jtbandes is correct. This is exactly what `NSInvocation` is for, and it restricts it correctly to ObjC types. There's no way to call a pure C function with unknown parameters. That would break the ABI. The types and number of parameters and return value impact what registers are used, what goes on the stack, etc. If the goal is some kind of generic swizzling, you're almost certainly on a dead-end. We didn't even have that in ObjC, and Swift will fight you tooth and nail. – Rob Napier Nov 28 '16 at 23:47

0 Answers0