steipete / Aspects

Delightful, simple library for aspect oriented programming in Objective-C and Swift.

Home Page:https://twitter.com/steipete

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hooking an optional delegate method that isn't implemented

avnerbarr opened this issue · comments

commented

Basically I want to add aspects to a datasource/delegate of a UITableView.
Obviously UITableView has lots of optional callbacks.

In many of my use cases the data source/delegate doesn't implement the optional methods, but I still need a way to forward the table view calls to my hooks.

In this example I need to return a different height for certain cells but I get an error "AspectErrorDoesNotRespondToSelector".

token = [object
           aspect_hookSelector:@selector(tableView:heightForRowAtIndexPath:)
           withOptions:AspectPositionInstead
           usingBlock:^(id <AspectInfo> info,UITableView *tableView,NSIndexPath *indexPath) {
                  // replace the original implementation with different calculation
                  // object doesn't implement the method...
                  // hook fails :(
             }error:&error];

Any suggestion how to go about adding the method dynamically to the class and then hooking?

Thanks

I'm running into the exact same scenario, with the added wrinkle of using Swift. Any advice would be greatly appreciated!

@avnerbarr I have the same problem. Have you solved it?

commented

You would need to add the method at runtime to the delegate class and then hook it. Which version of the library are you using? Perhaps this feature was added?