由 android 开发者在 2021_10_15 发布。
(OC比较难用,-_-||)
使用多个参数调用选择器,
你可以使用NSObject performSelector:withObject:withObject
,
但只支持传递两个参数!!!
幸运的是,你可以performSelector withObject X 3
通过objc_msgSend
函数来实现你。
#include <objc/message.h>
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2 withObject:(id)object3 {
typedef id (*send_type)(id, SEL, id, id, id);
send_type func = (send_type) objc_msgSend;
id retValue = func(self, aSelector, object1, object2, object3);
return retValue;
}
用法:
- (NSString *)ObjcMsgSendWithString:(NSString *)string withNum:(NSNumber *)number withArray:(NSArray *)array {
NSLog(@" ---> %@, %@, %@", string, number, array[0]);
return @"return 311";
}
- (void)test{
NSString *str = @"字符串objc_msgSend";
NSNumber *num = @20;
NSArray *arr = @[@"数组值1", @"数组值2"];
SEL sel = @selector(ObjcMsgSendWithString:withNum:withArray:);
NSLog(@"1223 ---> %@", [self performSelector:sel withObject:str withObject:num withObject:arr]);
}