I'm trying to implement method swizzling in swift 3 basing on answer How to implement method swizzling swift 3.0?
Here's my code:
// MARK: - Swizzling
private let swizzling: (UIView.Type) -> () = { view in
let originalSelector = #selector(view.awakeFromNib)
let swizzledSelector = #selector(view.swizzled_localization_awakeFromNib)
let originalMethod = class_getInstanceMethod(view, originalSelector)
let swizzledMethod = class_getInstanceMethod(view, swizzledSelector)
method_exchangeImplementations(originalMethod, swizzledMethod)
}
extension UIView {
open override class func initialize() {
guard self === UIView.self else {
return
}
swizzling(self)
}
func swizzled_localization_awakeFromNib() {
swizzled_localization_awakeFromNib()
if let localizableView = self as? Localizable {
localizableView.localize()
}
}
}
But on app launch it crashes with reason:
'-[UINavigationController swizzled_localization_awakeFromNib]: unrecognized selector sent to instance 0x7fc7c8820400'
I can't figure out why swizzled_localization_awakeFromNib called on UINavigationController. I am using this in obj-c project, can it be the reason? It worked fine in swift 2 throught dispatch_once.
I tried place breakpoint before swizzling(self), and it called once on UIView, as expected.