Last active
October 8, 2017 22:54
-
-
Save manicaesar/7ad167c7f5a7bdc1527dbe94862235fa 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
import Foundation | |
import ObjectiveC.runtime | |
@objc class Dummy: NSObject { | |
@objc dynamic func methodA() { print("methodA") } | |
@objc dynamic func methodB() { print("methodB") } | |
@objc func methodC() { print("methodC") } | |
@objc func methodD() { print("methodD") } | |
} | |
var dummy = Dummy() | |
let selectorA = #selector(Dummy.methodA) | |
let selectorB = #selector(Dummy.methodB) | |
let selectorC = #selector(Dummy.methodC) | |
let selectorD = #selector(Dummy.methodD) | |
let methodA = class_getInstanceMethod(Dummy.self, selectorA) | |
let methodB = class_getInstanceMethod(Dummy.self, selectorB) | |
let methodC = class_getInstanceMethod(Dummy.self, selectorC) | |
let methodD = class_getInstanceMethod(Dummy.self, selectorD) | |
method_exchangeImplementations(methodA!, methodB!) | |
method_exchangeImplementations(methodC!, methodD!) | |
dummy.methodA() // Prints methodB | |
dummy.methodB() // Prints methodA | |
dummy.perform(selectorA) // Prints methodB | |
dummy.perform(selectorB) // Prints methodA | |
dummy.methodC() // Prints methodC | |
dummy.methodD() // Prints methodD | |
dummy.perform(selectorC) // Prints methodD | |
dummy.perform(selectorD) // Prints methodC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment