Skip to content
Swizzle selectors with just one clean and elegant API
Swift Ruby
Branch: master
Clone or download
Latest commit 52ba2ee Jul 30, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.swiftpm/xcode/package.xcworkspace Move swift packages file to root folder Jul 30, 2019
Example
Sources/SwizzleSwift Move swift packages file to root folder Jul 30, 2019
.gitignore
LICENSE Initial commit Jul 26, 2019
Package.swift
README.md Update readme Jul 30, 2019
SwizzleSwift.png
SwizzleSwift.podspec Update podspec Jul 30, 2019
_Pods.xcodeproj

README.md

SwizzleSwift

Who said that method swizzling needs to look ugly? SwizzleSwift is a little wrapper that lets you swizzle selectors just one clean and elegant API.

SwizzleSwift: Swizzle selectors with just one clean and elegant API

Version License Platform

Installation

Pods

pod 'SwizzleSwift'

Swift package manager

From Xcode, select File → Swift Packages → Add Package Dependency → Select your project → Search SwizzleSwift

Usage example

Swizzle(<#AType.Self#>) {
	<#OriginalSelector#> ==> <#SwizzledSelector#>
}

Given the following controller

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        print(#function)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print(#function)
    }

}

extension UIViewController {

    @objc private func myViewDidLoad() {
        print(#function)
        myViewDidLoad()
    }
    
    @objc private func myViewWillAppear(_ animated: Bool) {
        print(#function)
        myViewWillAppear(animated)
    }
    
}

Swizzling the methods like this

extension UIViewController {

    @objc static func methodSwizzling() -> Void {
        Swizzle(ViewController.self) {
            #selector(viewDidLoad) ==> #selector(myViewDidLoad)
            #selector(viewWillAppear(_:)) ==> #selector(myViewWillAppear(_:))
        }
    }
    
}

Will produce the following output

myViewDidLoad()
viewDidLoad()
myViewWillAppear(_:)
viewWillAppear(_:)
You can’t perform that action at this time.