如何通過反射傳入基本數據類型參數?
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
NSNumber *appID = @(100);
Class class = NSClassFromString(@"xxx");
[class performSelector:NSSelectorFromString(@"類方法") withObject:appID];
直接使用上面的performSelector的方式是走不通的,可以使用NSInvocation。
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
//checkInputValueEqual:withError: 驗證兩次輸入內容是否一致
SEL mysel = NSSelectorFromString(@"checkInputValueEqual:withError:");
if ([codeView2 respondsToSelector:mysel]) {
NSInteger comparErrorCode = 0;
// 1、方法簽名
NSMethodSignature* signature = [codeView2 methodSignatureForSelector:mysel];
const char* retType = [signature methodReturnType];
if (strcmp(retType, @encode(BOOL)) == 0) {
//包裝方法
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
//方法調用者
invocation.target = codeView2;
//要調用的方法和方法簽名中的方法一樣
invocation.selector = mysel;
//設置傳遞的參數 0 代表target 1代表 selector 所以從2開始
[invocation setArgument:&firstTextField atIndex:2];
[invocation setArgument:&comparErrorCode atIndex:3];
//執行方法
[invocation invoke];
//獲取返回值
BOOL result = 0;
[invocation getReturnValue:&result];
if (result) {
if (weakself.closeBlock) {
weakself.closeBlock(encryptedData2, clientRandomEncryptedData2,weakself.param.strServerRandom2);
}
}else {
[weakself reloadCodeView:@"密碼輸入不一致,請重新輸入!"];
}
}
}
#pragma clang diagnostic pop