I am developing an Objective-C application, and what I want to do, is something like the following:
+-----------------+ +---------------+
| Some Object | <---------- | Synchronize |
|(Not Thread Safe)| | Proxy |
+-----------------+ / +---------------+
/
/ Intercepts [someobject getCount]
/ @synchronize (someObject)
/
[someObject getCount] /
+----------------------+
| Some Calling Object |
+----------------------+
What I’ve asking is, how can I create an object in objective-c, that intercepts messages sent to another object, in order to perform code before the message is sent to that object.
Some things that I think will not work:
- Categories (I need this to only happen for certain instances of a class)
- Rewriting the object (I don’t have access to the source of the object)
- Method swizzling (once again, this need to only happen for certain instances of a class)
So, I bit the bullet, and decided to make my own proxy class. To subclass, you simply override the ‘forwardInvocation:’ message, and you call any code you need there, before calling
[super forwardInvocation:]. Please not this will NOT work with vardic methods, as NSInvocation doesn’t work with vardic methods.