RunTime的一些知识点之方法
获取已注册 class 的数量
/*
* buffer class 数组值的缓存,所有注册的class 的数量, 通过传入null来获取所有的注册的class
* bufferCount 申请缓存空间指针的数量
* On return, this function fills in only this number of elements. If this number is less
* than the number of registered classes, this function returns an arbitrary subset of the registered classes.
*
*/
OBJC_EXPORT int objc_getClassList(Class *buffer, int bufferCount)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
例子
int numClasses;
Class *classes = NULL;
classes = NULL;
numClasses = objc_getClassList(NULL,0);
NSLog(@"Number of classes: %d", numClasses);
if (numClasses >0 )
{
classes = (__unsafe_unretained Class *)malloc(sizeof(Class) * numClasses);
numClasses = objc_getClassList(classes, numClasses);
for (int i = 0; i < numClasses; i++) {
NSLog(@"Class name: %s",class_getName(classes[i]));
}
free(classes);
}
获取注册类列表
OBJC_EXPORT Class *objc_copyClassList(unsigned int *outCount)
OBJC_AVAILABLE(10.7, 3.1, 9.0, 1.0);
unsigned int outCount;
Class *classes = objc_copyClassList(&outCount);
for (int i = 0; i < outCount; i++) {
NSLog(@"%s", class_getName(classes[i]));
}
free(classes);
获取class 名称
OBJC_EXPORT const char *class_getName(Class cls)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
是否为元类
OBJC_EXPORT BOOL class_isMetaClass(Class cls)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
获取父类
OBJC_EXPORT Class class_getSuperclass(Class cls)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
设置父类
/*
* 返回旧的的superclass
*/
OBJC_EXPORT Class class_setSuperclass(Class cls, Class newSuper)
__OSX_DEPRECATED(10.5, 10.5, "not recommended")
__IOS_DEPRECATED(2.0, 2.0, "not recommended")
__TVOS_DEPRECATED(9.0, 9.0, "not recommended")
__WATCHOS_DEPRECATED(1.0, 1.0, "not recommended");
获取class的版本信息
OBJC_EXPORT int class_getVersion(Class cls)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
设置版本信息
OBJC_EXPORT void class_setVersion(Class cls, int version)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
获取实例化大小
// bytes
OBJC_EXPORT size_t class_getInstanceSize(Class cls)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
获取实例化变量
OBJC_EXPORT Ivar class_getInstanceVariable(Class cls, const char *name)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
OBJC_EXPORT Ivar class_getClassVariable(Class cls, const char *name)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
获取实例变量列表
OBJC_EXPORT Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
获取实例化方法列表
OBJC_EXPORT Method class_getInstanceMethod(Class cls, SEL name)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
获取类方法列表
OBJC_EXPORT Method class_getClassMethod(Class cls, SEL name)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
获取方法的实现
OBJC_EXPORT IMP class_getMethodImplementation(Class cls, SEL name)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
判断方法是否响应
OBJC_EXPORT BOOL class_respondsToSelector(Class cls, SEL sel)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
拷贝方法列表
OBJC_EXPORT Method *class_copyMethodList(Class cls, unsigned int *outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
网友评论