Code Monkey home page Code Monkey logo

nvmaspects's Introduction

NVMAspects

About

Yet another AOP library for Objective-C, but implement by using 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, JSPatch, NSBlog and sunnyxx's blog, 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 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.

[[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, Union, Struct contain array. 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:

pod "NVMAspects"

License

NVMAspects is released under the MIT license.

nvmaspects's People

Contributors

axl411 avatar codelife2012 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

philcai1993

nvmaspects's Issues

Support union.

NSInvocation also can't support this, need modify libffi to support this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.