Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always return nil when swizzling #2

Open
TLHorse opened this issue Apr 3, 2020 · 0 comments
Open

Always return nil when swizzling #2

TLHorse opened this issue Apr 3, 2020 · 0 comments

Comments

@TLHorse
Copy link

TLHorse commented Apr 3, 2020

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant