I want to do something a little hacky.
When we try and call a method on a class where isn't defined we usually get an error, e.g.
// We get a undefined selector callback
[myClass someUndefinedMethod];
I want to add something into MyClass
that catches all these undefined method calls and deals with it. Is this possible?
I want something like this, but which will intercept all method calls:
@implementation MyClass
- (void) performSelector(SEL):selector {
// Check if the method exists
if (![self respondsToSelector:selector]) {
// Handle unimplemeted selector
NSLog(@"No method called %@", selector);
}
// Otherwise proced as normal
else {
[super performSelector:selector];
}
}
@end
Why not just override the doesNotRecognizeSelector:
method on NSObject (assuming you're inheriting from it, which you should be)?
Override method:
-[MyClass doesNotRecognizeSelector:]
and call whatever you want.
This is what NSManagedObject
is doing to get/set core data properties.
I'm not clear whether you are trying to intercept messages sent to a class or to an instance of a class. In any case perhaps looking into / searching on the topics of 'message forwarding' and 'forwarding messages'. Also see NSObject's forwardInvocation and the section of Apple's runtime pgmg guide on message forwarding http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtForwarding.html
These got me to the answer I was looking for and I did not see them mentioned elsewhere on this question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With