Created
January 28, 2016 09:52
-
-
Save AsceticMonk/487dab6f966a451b310a to your computer and use it in GitHub Desktop.
Swift instance method swizzling
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 | |
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