Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C and use of SEL/IMP

Tags:

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?

like image 811
SK9 Avatar asked Apr 16 '10 02:04

SK9


People also ask

What is SEL in Objective C?

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+

What is IMP Iphone?

An opaque type that represents an Objective-C declared property.


1 Answers

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?

like image 82
shosti Avatar answered Nov 28 '22 16:11

shosti