如何通過反射傳入基本數(shù)據(jù)類型參數(shù)

如何通過反射傳入基本數(shù)據(jù)類型參數(shù)?

- (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:  驗證兩次輸入內(nèi)容是否一致
            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];
                    //方法調(diào)用者
                    invocation.target = codeView2;
                        //要調(diào)用的方法和方法簽名中的方法一樣
                    invocation.selector = mysel;
                    //設(shè)置傳遞的參數(shù) 0 代表target 1代表 selector 所以從2開始
                    [invocation setArgument:&firstTextField atIndex:2];
                    [invocation setArgument:&comparErrorCode atIndex:3];
                    //執(zhí)行方法
                    [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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容