2009-08-10 42 views

回答

4

的Objective-C运行有一个称为“method_getName”与需要一个方法对象,并返回一个SEL功能。

1

另外,使用:

NSSelectorFromString(@"myMethodName"); 
0

你的头例子是不太清楚。

但是,我们走了。所有类的所有选择器都存在于同一个命名空间中。在类Baz的类Bar,或doFoo含义doFoo都将是相同的唯一选择。这意味着你不需要为了得到一个选择器而烦恼这个类。两个很好的方法来做到这一点。

NSSelectorFromString(@"doFoo"); // If you have the selector name as a string. 
@selector(foFoo); // If it is selector constant inlined in your code. 

你的问题也可以引用如何从方法返回选择器。由于选择器是obj-c中的第一类公民,我们可以将它们作为任何变量传递,并从方法中返回。选择器的类型是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