如何用Runtime获得一个类中有哪些参数,方法,协议?

如何获得一个类的参数列表,方法名列表,协议列表呢?

如下代码:  (可以得到参数列表)

-(NSArray *)getPropertyNameList{

NSMutableArray*mArray = [NSMutableArrayarray];

//拷贝属性列表class

/*

1.哪个类

2.count数量输出参数

*/

unsignedintcount;

/*

An array of pointers of type \c objc_property_t describing the properties

*  declared by the class.

返回一个数组的指针数组中的类型为objc_property_t

*/

objc_property_t*propertyList =class_copyPropertyList([selfclass], &count);

for(inti =0;i

//拿到数组中的每个属性objc_property_t

//property_getName获取属性的名字

constchar*pName =property_getName(propertyList[i]);

//char _>nsstring

NSString*pNameStr = [[NSStringalloc]initWithUTF8String:pName];

[mArrayaddObject:pNameStr];

}

return mArray.copy;

}

得到方法名列表:

#pragma mark

#pragma mark -得到方法名列表

-(NSArray *)getMethodList{

NSMutableArray*arrayM = [NSMutableArrayarray];

unsignedintcount;

Method*methodList =class_copyMethodList([selfclass],&count);

for (inti =0; i < count; i++) {

SELmName =method_getName(methodList[i]);

NSString*mNameStr =NSStringFromSelector(mName);

[arrayMaddObject:mNameStr];

}

return arrayM.copy;

}

得到协议列表:

#pragma mark

#pragma mark -得到协议列表

-(NSArray *)getProtocolList{

NSMutableArray*arrayM = [NSMutableArrayarray];

unsignedintcount;

Protocol *__unsafe_unretained* protocolList =class_copyProtocolList([selfclass],&count);

for (inti =0; i < count; i++) {

constchar*proName =protocol_getName(protocolList[i]);

NSString*proNameStr = [[NSStringalloc]initWithUTF8String:proName];

[arrayMaddObject:proNameStr];

}

return arrayM.copy;

}

一般来说会写一个NSObject的分类,来把这三个方法集成进去,直接进行调用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容