c_cpp 自动生成的对象描述

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 自动生成的对象描述相关的知识,希望对你有一定的参考价值。

//
//  Copyright © 2014 Yuri Kotov
//

#import "NSObject+ADVDescription.h"
#import <objc/runtime.h>

@implementation NSObject (ADVDescription)

- (NSString *) autoDescription
{
    Class class = self.class;

    NSMutableString *description =
        [NSMutableString stringWithFormat:@"<%@: %p> {\n",
                NSStringFromClass(class),
                (__bridge void *) self];
    do
    {
        unsigned int count = 0;
        objc_property_t *properties = class_copyPropertyList(class, &count);
        for (int i = 0; i < count; ++i)
        {
            @autoreleasepool
            {
                objc_property_t property = properties[i];
                const char *name = property_getName(property);
                NSString *key = [[NSString alloc] initWithBytesNoCopy:(void*)name
                                                               length:strlen(name)
                                                             encoding:NSASCIIStringEncoding
                                                         freeWhenDone:NO];
                id value = [self valueForKey:key];
                [description appendFormat:@"\t%@ = %@;\n", key, value];
            }
        }
        free(properties);
    }
    while ([NSObject class] != (class = [class superclass]));

    [description appendString:@"}"];

    return description;
}

@end
//
//  Copyright © 2014 Yuri Kotov
//

#import <Foundation/Foundation.h>

@interface NSObject (ADVDescription)

- (NSString *) autoDescription;

@end

以上是关于c_cpp 自动生成的对象描述的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp C中流对象的一个​​实现

c_cpp 功能语言的小测试对象

c_cpp 示例联合对象cpp

c_cpp CPP - 教程010 - 面向对象的编程

c_cpp 在OBJECT周围旋转对象

c_cpp C面向对象编程实例