Skip to content

Instantly share code, notes, and snippets.

@AsceticMonk
Created January 28, 2016 09:52
Show Gist options
  • Save AsceticMonk/487dab6f966a451b310a to your computer and use it in GitHub Desktop.
Save AsceticMonk/487dab6f966a451b310a to your computer and use it in GitHub Desktop.
Swift instance method swizzling
import Foundation
class FavoriteCity {
dynamic func currentWinner() -> String {
return "Vancouver"
}
dynamic func lastWinner() -> String {
return "Shanghai"
}
}
let myFavoriteCity = FavoriteCity()
print(myFavoriteCity.currentWinner())
// Let's swizzle
let aClass = FavoriteCity.self
let originalMethod = class_getInstanceMethod(aClass, "currentWinner")
let swizzledMethod = class_getInstanceMethod(aClass, "lastWinner")
method_exchangeImplementations(originalMethod, swizzledMethod)
print(myFavoriteCity.currentWinner())
print(myFavoriteCity.lastWinner())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment