iOS 获取类的全部属性和全部方法 +(用例拓展:MVC的数据解析==数据映射)

备注:

#import <objc/runtime.h>

1.获取类的全部属性

<span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html">/**
 *  获取类的所有属性
 */
+(void)showStudentClassProperties
{
    Class cls = [Student class];
    unsigned int count;
    while (cls!=[NSObject class]) {
        objc_property_t *properties = class_copyPropertyList(cls, &count);
        for (int i = 0; i<count; i++) {
            objc_property_t property = properties[i];
            NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
            NSLog(@"属性名==%@",propertyName);
        }
        if (properties){
            //要释放
           free(properties);
        }
    //得到父类的信息
    cls = class_getSuperclass(cls);
    }
}


 
<span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;">2.获取类的全部方法</span>

/**
 *  获取类的所有方法
 */
+(void)showStudentClassMethods
{
    unsigned int count;
    Class cls = [Student class];
    while (cls!=[NSObject class]){
        Method *methods = class_copyMethodList(cls, &count);
        for (int i=0; i < count; i++) {
            NSString *methodName = [NSString stringWithCString:sel_getName(method_getName(methods[i])) encoding:NSUTF8StringEncoding];
            NSLog(@"方法名:%@ ", methodName);
        }
        if (methods) {
             free(methods);
        }
        cls = class_getSuperclass(cls);
    }
   
}
3.获取类的全部方法
/**
 *  获取类的所有属性 做健值反射
 */
+(Student *)showStudentClassPropertiesToMapValueWithDic:(NSDictionary *)dic
{
    Student *stu = [[Student alloc] init];
    
    Class cls = [Student class];
    unsigned int count;
    while (cls!=[NSObject class]) {
        objc_property_t *properties = class_copyPropertyList(cls, &count);
        for (int i = 0; i<count; i++) {
            objc_property_t property = properties[i];
            NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
            //得到属性名可以在这里做反射 反馈过来的dic 直接反射成一个model 
            /**
             *  valueForKey: 是 KVC(NSKeyValueCoding) 的方法,在 KVC 里可以通过 property 同名字符串来获取对应的值
             *  这里的dic 的key 与 stu 的属性名一一对应
             * (MVC模式设计数据反射时候用到{类定义属性时候要和服务端反馈过来的字段一样})
             */
            id propertyValue = [dic valueForKey:propertyName];
            if (propertyValue)
                [stu setValue:propertyValue forKey:propertyName];//属性get set赋值
            // NSLog(@"%@ = %@",propertyName,propertyValue);
        }
        if (properties){
            free(properties);
        }
        //得到父类的信息
        cls = class_getSuperclass(cls);
    }
    return stu;
}


==========================================================================================================

全部代码

.h

//
//  Student.h
//  demo
//
//  Created by linpeng on 14-7-22.
//  Copyright (c) 2014年 linpeng. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface People : NSObject
/**
 *  名字
 */
@property(nonatomic,strong)NSString *name;
/**
 *  性别
 */
@property(nonatomic,strong)NSString *sex;
/**
 *  年龄
 */
@property(nonatomic,)NSInteger age;
@end

/**
 *  //
 */

@interface Student : People
/**
 *  学校
 */
@property(nonatomic,strong)NSString *school;
/**
 *  班级
 */
@property(nonatomic,strong)NSString *classroom;
@end



/**
 *  //
 */

@interface StudentManage : NSObject
+(void)showStudentClassProperties;
+(Student *)showStudentClassPropertiesToMapValueWithDic:(NSDictionary *)dic;
+(void)showStudentClassMethods;
@end












.m

//
//  Student.m
//  demo
//
//  Created by linpeng on 14-7-22.
//  Copyright (c) 2014年 linpeng. All rights reserved.
//

#import "Student.h"
#import <objc/runtime.h>
@implementation People
-(void)pers
{
    
}
@end

@implementation Student
-(void)show
{
    
}
-(void)hide
{
    
}
@end

@implementation StudentManage
/**
 *  获取类的所有属性
 */
+(void)showStudentClassProperties
{
    Class cls = [Student class];
    unsigned int count;
    while (cls!=[NSObject class]) {
        objc_property_t *properties = class_copyPropertyList(cls, &count);
        for (int i = 0; i<count; i++) {
            objc_property_t property = properties[i];
            NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
            NSLog(@"属性名==%@",propertyName);
        }
        if (properties){
            //要释放
           free(properties);
        }
    //得到父类的信息
    cls = class_getSuperclass(cls);
    }
}
/**
 *  获取类的所有属性 做健值反射
 */
+(Student *)showStudentClassPropertiesToMapValueWithDic:(NSDictionary *)dic
{
    Student *stu = [[Student alloc] init];
    
    Class cls = [Student class];
    unsigned int count;
    while (cls!=[NSObject class]) {
        objc_property_t *properties = class_copyPropertyList(cls, &count);
        for (int i = 0; i<count; i++) {
            objc_property_t property = properties[i];
            NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
            //得到属性名可以在这里做反射 反馈过来的dic 直接反射成一个model 
            /**
             *  valueForKey: 是 KVC(NSKeyValueCoding) 的方法,在 KVC 里可以通过 property 同名字符串来获取对应的值
             *  这里的dic 的key 与 stu 的属性名一一对应
             * (MVC模式设计数据反射时候用到{类定义属性时候要和服务端反馈过来的字段一样})
             */
            id propertyValue = [dic valueForKey:propertyName];
            if (propertyValue)
                [stu setValue:propertyValue forKey:propertyName];//属性get set赋值
            // NSLog(@"%@ = %@",propertyName,propertyValue);
        }
        if (properties){
            free(properties);
        }
        //得到父类的信息
        cls = class_getSuperclass(cls);
    }
    return stu;
}

/**
 *  获取类的所有方法
 */
+(void)showStudentClassMethods
{
    unsigned int count;
    Class cls = [Student class];
    while (cls!=[NSObject class]){
        Method *methods = class_copyMethodList(cls, &count);
        for (int i=0; i < count; i++) {
            NSString *methodName = [NSString stringWithCString:sel_getName(method_getName(methods[i])) encoding:NSUTF8StringEncoding];
            NSLog(@"方法名:%@ ", methodName);
        }
        if (methods) {
            //要释放
             free(methods);
        }
        //得到父类的信息
        cls = class_getSuperclass(cls);
    }
   
}
@end

=========================================

判别这个属性是否具备get set方法 (重要!以上都要添加这个判别)


            NSString *propertySetMethod = [NSString stringWithFormat:@"set%@%@:", [[propertyName substringToIndex:1] capitalizedString]

                                           ,[propertyName substringFromIndex:1]];

            SEL selector = NSSelectorFromString(propertySetMethod);

            if ([model respondsToSelector:selector])

            {

                [model setValue:propertyValue forKey:propertyName];

            }




使用:

 /**
     *  dic 服务端反馈过来的字典对象(学生基本信息属性)school name age classroom等等
     *  Student 面向对象的 学生model(属性和服务器端反馈过来的属性--key要一直:school name age classroom等等)
     */
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"厦门大学",@"school",@"屎壳郎情调",@"name",@24,@"age",nil];
    //dic===>model(student)
    Student *stu = [StudentManage showStudentClassPropertiesToMapValueWithDic:dic];
    NSLog(@"基本信息:%@",stu);
结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值