NSObject+Extras.m
1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// NSObject+Extras.m
// DreamSleep
//
// Created by peter on 2022/4/19.
//
#import "NSObject+Extras.h"
@implementation NSObject (Extras)
// 重写debugDescription, 而不是description
- (NSString *)debugDescription {
// 判断是否时NSArray 或者NSDictionary NSNumber 如果是的话直接返回 debugDescription
if ([self isKindOfClass:[NSArray class]] || [self isKindOfClass:[NSDictionary class]] || [self isKindOfClass:[NSString class]] || [self isKindOfClass:[NSNumber class]]) {
return [self debugDescription];
}
// 声明一个字典
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
// 得到当前class的所有属性
uint count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
// 循环并用KVC得到每个属性的值
for (int i = 0; i<count; i++) {
objc_property_t property = properties[i];
NSString *name = @(property_getName(property));
id value = [self valueForKey:name]?:@"nil"; // 默认值为nil字符串
[dictionary setObject:value forKey:name]; // 装载到字典里
}
// 释放
free(properties);
//return
return [NSString stringWithFormat:@"<%@: %p> -- %@", [self class], self,dictionary];
}
@end