GithubHelp home page GithubHelp logo

marioiannotta / swizzleswift Goto Github PK

View Code? Open in Web Editor NEW
155.0 7.0 17.0 173 KB

Swizzle selectors with just one clean and elegant API

License: MIT License

Swift 87.33% Ruby 12.67%
swift swizzling runtime

swizzleswift's Introduction

Hey there!

I'm just a guy who loves to code and has been doing it since I was 13. I wore many hats (EM, tech lead, IC, rubber duck) and experimented with different technologies but in the past years I've been focusing mostly on iOS.

Personal website | Twitter | [email protected]

swizzleswift's People

Contributors

marioiannotta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

swizzleswift's Issues

SwizzleFunctionBuilder returns SwizzlePair when arguments passed only one SwizzlePair

In SwizzleSwiftDemo, Xcode cannot build the following code because of an error Cannot convert value of type 'SwizzlePair' to closure result type '[SwizzlePair]'.

Swizzle(ViewController.self) {
    #selector(viewDidLoad) ==> #selector(myViewDidLoad)
}

Passing an array of SwizzlePair instead of @_functionBuilder may resolve this issue.

Swizzle(ViewController.self, [
    #selector(viewDidLoad) ==> #selector(myViewDidLoad),
    #selector(viewWillAppear(_:)) ==> #selector(myViewWillAppear(_:)),
])

Can only swizzle functions with @objc keyword

I tried to write a swizzle demo:

  • First, I defined a class which is called MyClass with some functions;
  • Then, I defined another class which is called MyClassHook. In this class with another set of functions;
  • I used functions of MyClassHook to swizzle MyClass.

BUT! The compiler says I must add @objc keyword at the functions of MyClass. But I don’t want to do that.

Is there a way that I can avoid this issue?

Swift Package Manager issues

I can't get this repo to work with SPM.

I get the following error:

The remote repository could not be accessed. Make sure a valid repository exists at the specified location and that the correct credentials have been supplied.

Even when I tried to fork it and update the versioning to semver.

Always return nil when swizzling

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.