-
Notifications
You must be signed in to change notification settings - Fork 172
/
NSObject+BBYDebugTool.m
212 lines (158 loc) · 6.2 KB
/
NSObject+BBYDebugTool.m
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// NSObject+BBYDebugTool.m
// BBYDebugTool
//
// Created by 毕博洋 on 2018/10/18.
// Copyright © 2018 毕博洋. All rights reserved.
//
#import "NSObject+BBYDebugTool.h"
@interface NSString (BBYDebugTool)
+ (NSString *)decodeType:(const char *)cString;
@end
@implementation NSString (BBYDebugTool)
+ (NSString *)decodeType:(const char *)cString {
if (!strcmp(cString, @encode(char))) return @"char";
if (!strcmp(cString, @encode(int))) return @"int";
if (!strcmp(cString, @encode(short))) return @"short";
if (!strcmp(cString, @encode(long))) return @"long";
if (!strcmp(cString, @encode(long long))) return @"long long";
if (!strcmp(cString, @encode(unsigned char))) return @"unsigned char";
if (!strcmp(cString, @encode(unsigned int))) return @"unsigned int";
if (!strcmp(cString, @encode(unsigned short))) return @"unsigned short";
if (!strcmp(cString, @encode(unsigned long))) return @"unsigned long";
if (!strcmp(cString, @encode(unsigned long long))) return @"unsigned long long";
if (!strcmp(cString, @encode(float))) return @"float";
if (!strcmp(cString, @encode(double))) return @"double";
if (!strcmp(cString, @encode(BOOL))) return @"BOOL";
if (!strcmp(cString, @encode(void))) return @"void";
if (!strcmp(cString, @encode(char *))) return @"char *";
if (!strcmp(cString, @encode(id))) return @"id";
if (!strcmp(cString, @encode(Class))) return @"class";
if (!strcmp(cString, @encode(SEL))) return @"SEL";
NSString *result = [NSString stringWithCString:cString encoding:NSUTF8StringEncoding];
if (([[result substringToIndex:1] isEqualToString:@"@"] && [result rangeOfString:@"?"].location == NSNotFound))
{
result = [[result substringWithRange:NSMakeRange(2, result.length - 3)] stringByAppendingString:@"*"];
}
else
{
if ([[result substringToIndex:1] isEqualToString:@"^"])
{
result = [NSString stringWithFormat:@"%@ *",
[NSString decodeType:[[result substringFromIndex:1] cStringUsingEncoding:NSUTF8StringEncoding]]];
}
}
if ([result isEqualToString:@"@?"])
{
// @?== block
result = [NSString stringWithFormat:@"Block"];
}
return result;
}
@end
static void getSuper(Class class, NSMutableString *result) {
[result appendFormat:@" -> %@", NSStringFromClass(class)];
if ([class superclass])
{
getSuper([class superclass], result);
}
}
@implementation NSObject (BBYDebugTool)
+ (NSArray *)BBY_LogProperty {
unsigned int outCount;
objc_property_t *properties = class_copyPropertyList([self class], &outCount);
NSMutableArray *result = [NSMutableArray array];
for (unsigned int i = 0; i < outCount; i++)
{
[result addObject:[self formattedPropery:properties[i]]];
}
free(properties);
return result.count ? [result copy] : nil;
}
- (void)BBY_LogProperty {
unsigned int propertyCount;
objc_property_t *propertyList = class_copyPropertyList([self class], &propertyCount);
for (unsigned int i = 0; i< propertyCount; i++)
{
objc_property_t property = propertyList[i];
NSLog(@"Property is %@",[NSString formattedPropery:property]);
}
}
+ (NSString *)formattedPropery:(objc_property_t)prop {
unsigned int attrCount;
objc_property_attribute_t *attrs = property_copyAttributeList(prop, &attrCount);
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
for (unsigned int idx = 0; idx < attrCount; idx++)
{
NSString *name = [NSString stringWithCString:attrs[idx].name encoding:NSUTF8StringEncoding];
NSString *value = [NSString stringWithCString:attrs[idx].value encoding:NSUTF8StringEncoding];
[attributes setObject:value forKey:name];
}
free(attrs);
NSMutableString *property = [NSMutableString stringWithFormat:@"@property "];
NSMutableArray *attrsArray = [NSMutableArray array];
[attrsArray addObject:[attributes objectForKey:@"N"] ? @"nonatomic" : @"atomic"];
if ([attributes objectForKey:@"&"])
{
[attrsArray addObject:@"strong"];
}
else if ([attributes objectForKey:@"C"])
{
[attrsArray addObject:@"copy"];
}
else if ([attributes objectForKey:@"W"])
{
[attrsArray addObject:@"weak"];
}
else
{
[attrsArray addObject:@"assign"];
}
if ([attributes objectForKey:@"R"])
{
[attrsArray addObject:@"readonly"];
}
if ([attributes objectForKey:@"G"])
{
[attrsArray addObject:[NSString stringWithFormat:@"getter=%@", [attributes objectForKey:@"G"]]];
}
if ([attributes objectForKey:@"S"])
{
[attrsArray addObject:[NSString stringWithFormat:@"setter=%@", [attributes objectForKey:@"G"]]];
}
[property appendFormat:@"(%@) %@ %@",[attrsArray componentsJoinedByString:@", "],[NSString decodeType:[[attributes objectForKey:@"T"]
cStringUsingEncoding:NSUTF8StringEncoding]],[NSString stringWithCString:property_getName(prop) encoding:NSUTF8StringEncoding]];
return [property copy];
}
+ (NSArray *)BBY_LogMethod {
u_int methodCount;
NSMutableArray *methodList = [NSMutableArray array];
Method *methods = class_copyMethodList([self class], &methodCount);
for (int i = 0; i < methodCount; i++)
{
SEL name = method_getName(methods[i]);
NSString *strName = [NSString stringWithCString:sel_getName(name) encoding:NSUTF8StringEncoding];
[methodList addObject:strName];
}
free(methods);
return [methodList copy];
}
- (void)BBY_LogMethod {
u_int methodCount;
NSMutableArray *methodList = [NSMutableArray array];
Method *methods = class_copyMethodList([self class], &methodCount);
for (int i = 0; i < methodCount; i++)
{
SEL name = method_getName(methods[i]);
NSString *strName = [NSString stringWithCString:sel_getName(name) encoding:NSUTF8StringEncoding];
[methodList addObject:strName];
}
free(methods);
NSLog(@"Method List is %@",methodList);
}
+ (NSString *)BBY_SuperClassLine {
NSMutableString *result = [NSMutableString string];
getSuper([self class], result);
return result;
}
@end