Skip to content

Instantly share code, notes, and snippets.

@hamishknight
Created July 13, 2017 15:32
Show Gist options
  • Save hamishknight/ea961b8aa360a13b9f1cb1a886a6eaab to your computer and use it in GitHub Desktop.
Save hamishknight/ea961b8aa360a13b9f1cb1a886a6eaab to your computer and use it in GitHub Desktop.
import Foundation
class C {
@objc /*dynamic*/ func foo() {
print("foo")
}
@objc /*dynamic*/ func bar() {
print("bar")
}
}
let fooMethod = class_getInstanceMethod(C.self, #selector(C.foo))
let barMethod = class_getInstanceMethod(C.self, #selector(C.bar))
method_exchangeImplementations(fooMethod, barMethod)
let c = C()
c.foo() // foo
c.bar() // bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment