重写description方法

打印属性列表

- (NSString *)description {

    u_int count;

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

    NSMutableString *descriptionString = [[NSMutableString alloc] initWithCapacity:0];

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

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

        if (i == 0) {

            [descriptionString appendString:@"{\n"];

        }

        NSString *key = [NSString stringWithUTF8String: propertyName];

        [descriptionString appendString:key];

        [descriptionString appendString:@" = "];

        [descriptionString appendString:[NSString stringWithFormat:@"%@",[self valueForKey:key]]];

        [descriptionString appendString:@";"];

        [descriptionString appendString:@"\n"];

        if (i == count - 1) {

            [descriptionString appendString:@"}"];

        }

    }

    free(properties);

    return descriptionString;

}

你可能感兴趣的:(重写description方法)