Last active
March 16, 2020 12:13
-
-
Save jtbandes/296e74387d54e6d72d48 to your computer and use it in GitHub Desktop.
This file contains 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
// please... don't do this | |
import Foundation | |
class F: NSObject | |
{ | |
@objc func foo() { | |
print("hi") | |
} | |
} | |
class C: NSObject { } | |
func addMethod(c: AnyClass, _ sel: Selector, types: String, usingBlock block: @convention(block) (AnyObject, AnyObject) -> Unmanaged<AnyObject>!) | |
{ | |
types.withCString { (typeStr: UnsafePointer<Int8>) -> Void in | |
class_addMethod(c, sel, imp_implementationWithBlock(unsafeBitCast(block, AnyObject.self)), typeStr) | |
return | |
} | |
} | |
addMethod(C.self, "methodSignatureForSelector:", types: "@@::") { receiver, arg -> Unmanaged<AnyObject>! in | |
print("getting signature") | |
return F().performSelector("methodSignatureForSelector:", withObject: arg as AnyObject!) | |
} | |
addMethod(C.self, "forwardInvocation:", types: "v@:@") { receiver, arg -> Unmanaged<AnyObject>! in | |
print("forwarding") | |
arg.performSelector("invokeWithTarget:", withObject: F()) | |
return nil | |
} | |
C().performSelector("foo") // prints "hi" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment