The main difficulty is to get the Selector from Swift and the NSMethodSignature and Block invocation, if you are interested.

Address: github.com/woshiccm/As…

Aspect is a lightweight, pure-Swift library for for aspect oriented programming. This project is heavily inspired by the popular Aspects. It provides you a chance to use a pure-Swift alternative in your next app.

Features

  • Hook object selector
  • Provide a more friendly Swift interface

Usage

Hook object selector with OC block

public class Test: NSObject {
   @objc dynamic func test(id: Int, name: String) {
        print(id)
        print(name)
    }
}

let test = Test()

let wrappedBlock: @convention(block) (AspectInfo, Int, String) -> Void = { aspectInfo, id, name in

}
let block: AnyObject = unsafeBitCast(wrappedBlock, to: AnyObject.self)
test.hook(selector: #selector(Test.test(id:name:)), strategy: .before, block: )
Copy the code

Hook object selector with Swift block

test.hook(selector: #selector(Test.test(id:name:)), strategy: .before) { (aspectInfo: AspectInfo, id: Int, name: String) in

}

Copy the code

Requirements

  • IOS 8.0 +
  • Swift, 4.0 5 x

Next Steps

  • Support hook class selector
  • Improve detail
  • Support Cocopods install

Installation

Carthage

Add the following line to your Cartfile

git "https://github.com/woshiccm/Aspect.git" "master"
Copy the code

Contributors

Backers

License

Aspect is released under the MIT license. See LICENSE for details.