Always return nil when swizzling
TLHorse opened this issue · 0 comments
TLHorse commented
I wrote the code:
class MyClass {
@objc func origA() {
print(#function)
}
@objc func origB(_ arg1: Bool) {
print(#function)
}
}
class MyClassHook {
@objc func myA() {
print(#function)
}
@objc func myB(_ arg1: Bool) {
print(#function)
}
}
MyClass().origA() // prints: origA()
MyClass().origB() // prints: origB()
Swizzle(MyClass.self) {
#selector(MyClass.origA) <-> #selector(MyClassHook.myA)
#selector(MyClass.origB(_:)) <-> #selector(MyClassHook.myB(_:))
}
MyClass().origA() // prints: origA(), expected: myA()
MyClass().origB() // prints: origB(), expected: myB()
I swizzle the method in right way. After swizzle, I print origA()
and origB()
, I expected them to be myA()
and myB()
, but the console still print origA()
and origB()
.
I run in debug mode and found that, in SwizzleSwift.swift
, public struct Swizzle
, there’ s a code;
guard
let original = class_getInstanceMethod(type, SwizzlePair.original),
let swizzled = class_getInstanceMethod(type, SwizzlePair.swizzled)
else { return }
And this code always execute the else
block, which means that original
or swizzled
is returning nil.
Why? Is there a problem in my code?