iOS 查詢一個(gè)類的所有成員變量和私有函數(shù)

iOS運(yùn)行時(shí)獲取對(duì)象的成員變量和成員方法

  1. 首先導(dǎo)入runtime 頭文件 #import<objc/runtime.h>

2.獲取某個(gè)類的成員變量或則屬性

 unsigned int numIvars;//成員變量個(gè)數(shù)

 Ivar *vars = class_copyIvarList(NSClassFromString(@"UIView"), &numIvars);

//Ivar *vars = class_copyIvarList([UIView class], &numIvars);

 NSString*key = nil;

 for(int i =0; i < numIvars; i++) {

   Ivar thisIvar = vars[i];

   key = [NSString stringWithUTF8String:ivar_getName(thisIvar)];//獲取成員變量的名字

   NSLog(@"variable name :%@", key);

   key = [NSString stringWithUTF8String:ivar_getTypeEncoding(thisIvar)];//獲取成員變量的數(shù)據(jù)類型

   NSLog(@"variable type :%@", key);
 }
 free(vars);

3.獲取成員函數(shù)

Method*meth =class_copyMethodList(NSClassFromString(@"UIView"), &numIvars);

//Method *meth = class_copyMethodList([UIView class], &numIvars);

   for(int i =0; i < numIvars; i++) {

     Method thisIvar = meth[i];

     SEL sel =method_getName(thisIvar);

     const char *name =sel_getName(self);

     NSLog(@"zp method :%s", name);

   }

   free(meth);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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