Is it possible?
views:
51answers:
3
Q:
Getting the selector from a method directly? NSSelectorFromMethod([[SomeClass someObject] Method]) ?
+1
A:
The Objective-C runtime has a function called "method_getName" with takes a Method object and returns a SEL.
Dave DeLong
2009-08-15 19:04:51
A:
Your example in the header is not quite clear.
But here we go. All selectors for all classes lives int he same namespace. Meaning doFoo
on the class Bar
, or doFoo
on the class Baz
will both be the same unique selector. This means you do not need to bother with the class in order to get a selector. Two nice ways to do it.
NSSelectorFromString(@"doFoo"); // If you have the selector name as a string.
@selector(foFoo); // If it is selector constant inlined in your code.
Your question could also refer to how to return selectors from a method. Since selectors are first class citizens in obj-c, we can pass them around as any variables, and return them from methods. The type of a selector is SEL
.
-(SEL)selectorFromFoo:(Foo*)aFoo; // Declare a method returning a selector.
SEL sel = [myBar selectorFromFoo:myFoo]; // Get a selector.
[myBar proformSelector:sel withObject:nil]; // Perform this selector
PeyloW
2009-11-10 15:19:08