Another question of mine about optimizing Objective C programs inspired the following: does anyone have a short example using SEL and IMP when theMethod has two (or more) integers for input?
Defines an opaque type that represents a method selector. iOS 4.0+ iPadOS 4.0+ macOS 10.6+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+
An opaque type that represents an Objective-C declared property.
Here's a good tutorial for getting the current IMP (with an overview of IMPs). A very basic example of IMPs and SELs is:
- (void)methodWithInt:(int)firstInt andInt:(int)secondInt { NSLog(@"%d", firstInt + secondInt); } SEL theSelector = @selector(methodWithInt:andInt:); IMP theImplementation = [self methodForSelector:theSelector]; //note that if the method doesn't return void, you have to explicitly typecast the IMP, e.g. int(* foo)(id, SEL, int, int) = ...
You could then invoke the IMP like so:
theImplementation(self, theSelector, 3, 5);
There's usually no reason to need IMPs unless you're doing serious voodoo--is there something specific you want to do?
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