2009-08-10 19 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