主要難點是Swift中拿到Selector以及Block的NSInvocation和NSMethodSignature, 感興趣的能夠看看。git
地址:github.com/woshiccm/As…github
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.bash
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: )
複製代碼
test.hook(selector: #selector(Test.test(id:name:)), strategy: .before) { (aspectInfo: AspectInfo, id: Int, name: String) in
}
複製代碼
Add the following line to your Cartfileapp
git "https://github.com/woshiccm/Aspect.git" "master"
複製代碼
Aspect is released under the MIT license. See LICENSE for details.ide