Aspects icon indicating copy to clipboard operation
Aspects copied to clipboard

Hooking an optional delegate method that isn't implemented

Open avnerbarr opened this issue 7 years ago • 3 comments

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

avnerbarr avatar Jan 01 '17 18:01 avnerbarr

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

allewun avatar Apr 26 '17 03:04 allewun

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

will0321 avatar May 14 '18 07:05 will0321

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?

avnerbarr avatar May 27 '18 12:05 avnerbarr