附录:runtime函数

2020-06-22  本文已影响0人  juriau

获得对象的类名

const char* object_getClassName(id obj)

获得方法数组(methodList)

Method* class_copyMethodList(Class cls, unsigned int * outCount) 
- (void)getMethods:(Class) cls{
    unsigned int count;
    Method* methodList = class_copyMethodList(cls, &count);
    
    NSMutableArray* arr = [NSMutableArray array];
    for (int i=0; i<count; i++) {
        Method method = methodList[i];
        SEL sel = method_getName(method);
        [arr addObject:NSStringFromSelector(sel)];
    }
    free(methodList);
    
    NSLog(@"%@", cls);
    NSLog(@"%@", arr);
}

获得变量数组(ivarList)

Ivar* class_copyIvarList(Class cls, unsigned int* outCount) 
var count:UInt32 = 0
let result = class_copyIvarList(UIGestureRecognizer.self, &count)!
for i in 0..<count{
    let namePointer = ivar_getName(result[Int(i)])!
    let name = String(cString: namePointer)
    print(name)
}
上一篇 下一篇

猜你喜欢

热点阅读