Latest 0.1.0
Homepage https://github.com/onmyway133/Warlock
License MIT
Platforms ios 8.0, requires ARC
Authors

Method swizzling in Swift

[![CI Status](http://img.shields.io/travis/Khoa Pham/Warlock.svg?style=flat)](https://travis-ci.org/Khoa Pham/Warlock)
Version
License
Platform
Carthage compatible

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

extension ViewController {
    override class func initialize() {
        struct Static {
            static var token: dispatch_once_t = 0
        }

        // make sure this isn't a subclass
        if self !== ViewController.self {
            return
        }

        dispatch_once(&Static.token) {
            Warlock.swizzleInstance(ViewController.self, original: Selector("viewWillAppear:"), swizzled: Selector("ftg_viewWillAppear:"))

            Warlock.swizzleInstance(ViewController.self, original: Selector("viewDidAppear:")) {
                print("viewDidAppear is called")
            }
        }
    }

    func ftg_viewWillAppear(animated: Bool) {
        self.ftg_viewWillAppear(animated)
        print("swizzled method is called")
    }
}

Features

  • Swizzle class and instance methods
  • Swizzle method with another method
 Warlock.swizzleInstance(ViewController.self, original: Selector("viewWillAppear:"), swizzled: Selector("ftg_viewWillAppear:"))
  • Swizzle method with block
Warlock.swizzleInstance(ViewController.self, original: Selector("viewDidAppear:")) {
    print("viewDidAppear is called")
}

Installation

Warlock is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "Warlock"

Credit

Credit goes to http://nshipster.com/swift-objc-runtime/

Author

Khoa Pham, [email protected]

License

Warlock is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "Warlock",
    "version": "0.1.0",
    "summary": "Method swizziling in Swift",
    "homepage": "https://github.com/onmyway133/Warlock",
    "license": "MIT",
    "authors": {
        "Khoa Pham": "[email protected]"
    },
    "source": {
        "git": "https://github.com/onmyway133/Warlock.git",
        "tag": "0.1.0"
    },
    "social_media_url": "https://twitter.com/onmyway133",
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "Pod/Classes/**/*",
    "resource_bundles": {
        "Warlock": [
            "Pod/Assets/*.png"
        ]
    }
}

Pin It on Pinterest

Share This