我正在为 IOS 开发一个库,它在某些时候需要NSArray
从各种类创建一个方法引用。到某个会在某个时候调用它们的单个类。
任何想法如何做到这一点?
谢谢
You have some options. You can use NSStrings
to actually have the name of your methods and then just do:
NSString *myMethodName = @"hellWorld";
SEL selector = selectorFromString(myMethodName);
You can then add your selector to your NSArray
.
You can also use NSInvocations
and store the methods and define the target later:
An NSInvocation object contains all the elements of an Objective-C message: a target, a selector, arguments, and the return value. Each of these elements can be set directly, and the return value is set automatically when the NSInvocation object is dispatched.
From Apple Documentation.
Edit 1 (for the fun of it):
- (Method *)getMethodsPointer{
unsigned int number = 0;
Method *myMethods = class_copyMethodList([self class], &number);
//for(int i=0;i<number;i++)
// {
// printf("%s",sel_getName(method_getName(myMethods[i])));
//}
return myMethods;
}
您可以将选择器添加到您的array
. 您可以像这样创建一个选择器:
-(void)myMethod:(int)something
SEL selectorToMyMethod = @selector(myMethod:);
希望有帮助,加油!
You could create some NSInvocations, store the Selectors and the target classes in it, and add the NSInvocation to the Array.
Storing the selectors only is possible, but only if the methods are in one class only - because Selectors are independant from their class.