iOS --如何方便查看model 里面的字段

如果我们的类中没有实现description 和debugDescription,当我们NSLog或者po的时候,输出的仅仅是一个内存地址,如下(lldb) po animal    <Animal:0x608000003cb0>(lldb):,这样对我们查看model里面的字段是很不方便的,因此我们需要打印属性的类可以实现下面的方法

- (NSString *)description{

return [NSString stringWithFormat:@"%@",[self properties_values]];

}

- (NSString *)debugDescription{

return [NSString stringWithFormat:@"%@",[self properties_values]];

}

#pragma mark---获取对象的所有属性 以及属性值

- (NSDictionary *)properties_values

{

NSMutableDictionary *props = [NSMutableDictionary dictionary];

unsigned int outCount, i;

objc_property_t *properties = class_copyPropertyList([self class], &outCount);

for (i = 0; i<outCount;i++)

{

objc_property_t property = properties[i];

const char* char_f =property_getName(property);

NSString *propertyName = [NSString stringWithUTF8String:char_f];

id propertyValue = [self valueForKey:(NSString *)propertyName];

if (propertyValue) [props setObject:propertyValue forKey:propertyName];

}

free(properties);

return props;

}

注意:需要导入头文件--#import<objc/runtime.h>,因为获取对象的所有属性 以及属性值是利用运行时实行的,下面是利用运行时遍历对象的属性和方法

1、/* 获取对象的所有属性,不包括属性值 */

- (NSArray *)getAllProperties

{

u_int count;

objc_property_t *properties  =class_copyPropertyList([self class], &count);

NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];

for (int i = 0; i<count; i++)

{

const char* propertyName =property_getName(properties[i]);

[propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];

}

free(properties);

return propertiesArray;

}


2、/* 获取对象的所有属性 以及属性值 */

- (NSDictionary *)properties_aps

{

NSMutableDictionary *props = [NSMutableDictionary dictionary];

unsigned int outCount, i;

objc_property_t *properties = class_copyPropertyList([self class], &outCount);

for (i = 0; i<outCount; i++)

{

objc_property_t property = properties[i];

const char* char_f =property_getName(property);

NSString *propertyName = [NSString stringWithUTF8String:char_f];

id propertyValue = [self valueForKey:(NSString *)propertyName];

if (propertyValue) [props setObject:propertyValue forKey:propertyName];

}

free(properties);

return props;

}


3、/* 获取对象的所有方法 */

-(void)printMothList

{

unsigned int mothCout_f =0;

Method* mothList_f = class_copyMethodList([self class],&mothCout_f);

for(int i=0;i<mothCout_f;i++)

{

Method temp_f = mothList_f[i];

IMP imp_f = method_getImplementation(temp_f);

SEL name_f = method_getName(temp_f);

const char* name_s =sel_getName(method_getName(temp_f));

int arguments = method_getNumberOfArguments(temp_f);

const char* encoding =method_getTypeEncoding(temp_f);

NSLog(@"方法名:%@,参数个数:%d,编码方式:%@",[NSString stringWithUTF8String:name_s],

arguments,[NSString stringWithUTF8String:encoding]);

}

free(mothList_f);

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,827评论 0 9
  • 本篇文章在《iOS开发之Runtime常用示例总结》基础上修改,地址是「:」http://www.cocoachi...
    小__小阅读 1,850评论 1 3
  • runTime 是ios底层运行时机制,他可以动态获取类的成员属性,变量,更可以改变这些东西! 要使用runTim...
    小心眼虎虎阅读 355评论 0 0
  • 爬行技能熟练之后,娃就得了这种病。 不管之前在干嘛、玩儿得有多投入,只要妈妈一进厨房,立刻就要过来抱大腿。 行为触...
    懒妈邦阅读 460评论 0 1
  • 冉冉 文 之前看过驴得水的话剧,只是觉得有些低俗的情节太露骨,不太明白这幕剧想表达的深意 。看过了驴得水的电影,心...
    冉冉ranran阅读 552评论 0 0