美文网首页
RunTime API

RunTime API

作者: KevinChein | 来源:发表于2020-05-19 10:06 被阅读0次

一、 获取方法列表----class_copyMethodList
method_getName、sel_getName

NSArray* MethodsOfClass(Class cls){
    NSMutableArray *methodObjs = @[].mutableCopy;
    unsigned int methodCount = 0;
    Method *methods = class_copyMethodList(cls, &methodCount);
    if (methods) {
        for (unsigned int i =0; i <methodCount; i++) {
            SEL sel = method_getName(methods[i]);
            const char *name = sel_getName(sel);
            NSString *nameString ;
            if (name) {
                nameString = [NSString stringWithUTF8String:name];
            }
            [methodObjs addObject:nameString];
        }
     }
    return methodObjs;
}

二、 获取属性列表 class_copyIvarList

ivar_getName、ivar_getTypeEncoding

  - (void)iVarGetClassNameList:(Class)cls{
    unsigned int outCount; // 1
    Ivar *ivars = class_copyIvarList(cls, &outCount); // 2

    for (int i = 0; i < outCount; i++) { // 3
        Ivar ivar = ivars[i]; // 4
        const char *ivarName = ivar_getName(ivar); // 5
        const char *ivarType = ivar_getTypeEncoding(ivar); // 6
        NSLog(@"实例变量名为:%s 字符串类型为:%s", ivarName,ivarType); // 7
    } // 8
    free(ivars);
}

相关文章

网友评论

      本文标题:RunTime API

      本文链接:https://www.haomeiwen.com/subject/xdevohtx.html