https://github.com/eleme/nvmaspects
https://github.com/eleme/nvmaspects
Last synced: 7 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/eleme/nvmaspects
- Owner: eleme
- License: mit
- Created: 2017-04-17T07:15:27.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-05T11:02:05.000Z (almost 8 years ago)
- Last Synced: 2025-05-30T03:48:11.236Z (8 months ago)
- Language: C
- Size: 537 KB
- Stars: 15
- Watchers: 18
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NVMAspects
## About
Yet another AOP library for Objective-C, but implement by using [libffi](https://github.com/libffi/libffi/). The main goal for this lib is to avoid heavily use of `forwardInvocation`, powerful, easy to use and also easy to read.
This lib is inspired by [Aspects](https://github.com/steipete/Aspects), [JSPatch](https://github.com/bang590/JSPatch), [NSBlog](https://www.mikeash.com/pyblog/) and [sunnyxx's blog](http://blog.sunnyxx.com/archives/), thanks all these people.
## Example
A simple example is look like this, provide a block to replace the original implementation, look very similar to [Aspects](https://github.com/steipete/Aspects) but have a bit difference. You can alter the arguments or return value by modify `info.invocation`. If you aren't sure the existing of original implementation, you should call `class_addPlaceholderIfNoImplement` fisrt.
```objective-c
[[UIImage nvm_hookInstanceMethod:@selector(imageNamed:)
usingBlock:^void(NVMAspectInfo *info, NSString *name) {
NSLog(@"Load image named %@", name);
[info.invocation invoke];
}];
```
## Notes
- Actually there are some bugs on this lib, [Bit Field](https://github.com/eleme/NVMAspects/issues/3), [Union](https://github.com/eleme/NVMAspects/issues/2), [Struct contain array](https://github.com/eleme/NVMAspects/issues/1). But `bit field` and `union` are also not well supportted by apple's `NSInvocation`, struct contain small array is a bug in `libffi`. All these features used very rare in Objective-C, so it's not a big problem.
- NVMAspects doesn't forbidden you to hook any method, but if you make some changes to method like `alloc`, you should follow the memory management policy, especially in `ARC`.
- Currently if a class has defined a method, hook this method will change it's `imp`, even if that method is implemented by it's super class. But this is not a big problem, you can distinguish the class by test `[self class]` in you block. If the class has not defined a method, you should call `class_addPlaceholderIfNoImplement` first.
- For problem caused by `forwardInvocation`, you can use this keyword to search issues in `Aspects` or `JSPatch`.
## Installation
add the following line to your Podfile:
```ruby
pod "NVMAspects"
```
## License
NVMAspects is released under the MIT license.