-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNSObject+Explain.m
More file actions
178 lines (128 loc) · 5.15 KB
/
NSObject+Explain.m
File metadata and controls
178 lines (128 loc) · 5.15 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
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
/*
* Copyright 2012 Florian Agsteiner
*/
#import "NSObject+Explain.h"
#import <Foundation/NSExpression.h>
#import <Foundation/NSString.h>
#import <Foundation/NSDate.h>
#import <Foundation/NSNull.h>
#import <CoreData/NSManagedObject.h>
#import <CoreData/NSManagedObjectID.h>
/**
* The non ascii version looks nicer but may be not supported depending on where you want to use it.
*/
#define FAExplainASCIIOnly 0
@implementation NSObject (Explain)
- (NSString*)explainDescription{
return [NSString stringWithFormat: @"%@ <%p>", (id)[self class], (void*)self];
}
+ (void)performRecursiveTreeDescriptionWithNode:(id)node output:(NSMutableString*)output prefix:(NSString*)prefix isLast:(BOOL)isLast
recursiveBlockReturningChildren:(NSString* (^)(id node, NSArray** children)) recursiveBlock{
NSArray* children = nil;
NSString* description = recursiveBlock(node,&children);
#if FAExplainASCIIOnly
[output appendFormat:@"%@%@%@\n",prefix, @"+-- ",description];
#else
[output appendFormat:@"%@%@%@\n",prefix, (isLast ? @"└── " : @"├── "),description];
#endif
if ([children conformsToProtocol:@protocol(NSFastEnumeration)]) {
if ([children count] > 0) {
#if FAExplainASCIIOnly
prefix = [prefix stringByAppendingString:(isLast ? @" " : @"| ")];
#else
prefix = [prefix stringByAppendingString:(isLast ? @" " : @"│ ")];
#endif
NSUInteger index = 0;
NSUInteger last = [children count] -1;
for (id child in children) {
BOOL isLast = (index == last);
[self performRecursiveTreeDescriptionWithNode:child output:output prefix:prefix isLast:isLast recursiveBlockReturningChildren:recursiveBlock];
index++;
}
}
}
else if(children != nil){
prefix = [prefix stringByAppendingString:(isLast ? @" " : @"│ ")];
[self performRecursiveTreeDescriptionWithNode:children output:output prefix:prefix isLast:YES recursiveBlockReturningChildren:recursiveBlock];
}
}
+ (NSString*)recursiveTreeDescriptionWithRoot:(id)root recursiveBlockReturningChildren:(NSString* (^)(id node, NSArray** children)) recursiveBlock{
NSMutableString* description = [NSMutableString stringWithCapacity:1000];
[self performRecursiveTreeDescriptionWithNode:root output:description prefix:@"" isLast:YES recursiveBlockReturningChildren:recursiveBlock];
return description;
}
+ (NSString*)recursiveTreeDescriptionWithRoot:(id)root descriptionSelector:(SEL)descriptionSelector recursiveChildrenSelector:(SEL)recursiveChildrenSelector{
return [self recursiveTreeDescriptionWithRoot:root recursiveBlockReturningChildren:^(id node, NSArray **children) {
id description = nil;
if ([node respondsToSelector:descriptionSelector]) {
description = [node performSelector:descriptionSelector];
if (description == nil){
description = [node description];
}
}
else{
description = [node description];
}
if ([node respondsToSelector:recursiveChildrenSelector]) {
*children = [node performSelector:recursiveChildrenSelector];
}
return description;
}];
}
- (NSString*)recursiveTreeDescriptionWithRecursiveBlockReturningChildren:(NSString* (^)(id node, NSArray** children)) recursiveBlock{
NSMutableString* description = [NSMutableString stringWithCapacity:1000];
[[self class] performRecursiveTreeDescriptionWithNode:self output:description prefix:@"" isLast:YES recursiveBlockReturningChildren:recursiveBlock];
return description;
}
- (NSString*)recursiveTreeDescriptionWithDescriptionSelector:(SEL)descriptionSelector recursiveChildrenSelector:(SEL)recursiveChildrenSelector{
return [[self class] recursiveTreeDescriptionWithRoot:self descriptionSelector:descriptionSelector recursiveChildrenSelector:recursiveChildrenSelector];
}
@end
@implementation NSDate (Explain)
- (NSString*)explainDescription{
return [self description];
}
@end
@implementation NSString (Explain)
- (NSString*)explainDescription{
return [self description];
}
@end
@implementation NSValue (Explain)
- (NSString*)explainDescription{
return [self description];
}
@end
@implementation NSNumber (Explain)
- (NSString*)explainDescription{
return [self description];
}
@end
@implementation NSNull (Explain)
- (NSString*)explainDescription{
return [self description];
}
@end
@implementation NSManagedObjectID (Explain)
- (NSString*)explainDescription{
return [self description];
}
@end
@implementation NSManagedObject (Explain)
- (NSString*)explainDescription{
return [NSString stringWithFormat:@"%@ <%p> %@", (id)[self class], (void*)self, [[self objectID] description]];;
}
@end
@implementation NSExpression (Explain)
- (NSString*)explainDescription{
NSString* description = nil;
if (self.expressionType == NSConstantValueExpressionType) {
id value = [self constantValue];
description = (value == nil)? @"nil" : [value explainDescription];
}
else{
description = [self description];
}
return description;
}
@end