备注:
#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);
结果