美文网首页iOS移动开发社区iOS学习笔记征服iOS
iOS 获取某个类的全部属性以及方法

iOS 获取某个类的全部属性以及方法

作者: love紫川 | 来源:发表于2016-03-07 22:14 被阅读4513次

在我们的工作中有时候需要获取某些sdk未提供的属性以及方法 ,这个时候应该怎么办呢?
可以通过以下方法获取

  • 获取类的方法
    + (void)LogAllMethodsFromClass:(id)obj
    {
    u_int count;
    //class_copyMethodList 获取类的所有方法列表
    Method *mothList_f = class_copyMethodList([obj class],&count) ;
    for (int i = 0; i < count; i++) {
    Method temp_f = mothList_f[i];
    // method_getImplementation 由Method得到IMP函数指针
    IMP imp_f = method_getImplementation(temp_f);

            // method_getName由Method得到SEL
            SEL name_f = method_getName(temp_f);
      
            const char * name_s = sel_getName(name_f);
            // method_getNumberOfArguments  由Method得到参数个数
            int arguments = method_getNumberOfArguments(temp_f);
            // method_getTypeEncoding  由Method得到Encoding 类型
            const char * encoding = method_getTypeEncoding(temp_f);
      
            NSLog(@"方法名:%@\n,参数个数:%d\n,编码方式:%@\n",[NSString stringWithUTF8String:name_s],
            arguments,[NSString stringWithUTF8String:encoding]);
        }
        free(mothList_f);
    
    }
    
  • 获取类的属性
    + (NSArray *)getAllProperties:(id)obj
    {
    u_int count;

      //使用class_copyPropertyList及property_getName获取类的属性列表及每个属性的名称
    
        objc_property_t *properties  =class_copyPropertyList([obj class], &count);
        NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
        for (int i = 0; i<count; i++)
        {
            const char* propertyName =property_getName(properties[i]);
            NSLog(@"属性%@\n",[NSString stringWithUTF8String: propertyName]);
            [propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];
        }
        free(properties);
        return propertiesArray;
    }

相关文章

网友评论

    本文标题:iOS 获取某个类的全部属性以及方法

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