-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSObject+Utilities.mm
170 lines (138 loc) · 5.46 KB
/
NSObject+Utilities.mm
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
//
// author: fabrice truillot de chambrier
//
// © 2010-2015, men in silicium sàrl
//
#import "OS/NSObject+Utilities.h"
#import "OS/OS.h"
@implementation NSObject (Utilities)
+ (instancetype) cast: (id) object
{
if ( [object isKindOfClass: self] ) return object;
return nil;
}
- (instancetype) castTo: (Class) type
{
if ( [self isKindOfClass: type] ) return self;
return nil;
}
+ (id) convert: (id) object
{
if ( object == nil ) return nil;
if ( [object isKindOfClass: self] ) return object;
if ( [object isKindOfClass: NSString.class] )
{
if ( self == NSString.class ) return object;
if ( self == NSNumber.class ) return [[NSDecimalNumber alloc] initWithString: object];
if ( self == NSDecimalNumber.class ) return [[NSDecimalNumber alloc] initWithString: object];
if ( self == NSDate.class )
{
NSDate* date = [object dateJSON];
if ( date == nil ) date = [object dateRFC1123];
return date;
}
}
if ( [object isKindOfClass: NSNumber.class] )
{
if ( self == NSString.class ) return [object stringValue];
if ( self == NSNumber.class ) return object;
if ( self == NSDecimalNumber.class ) return [[NSDecimalNumber alloc] initWithString: [object stringValue]];
if ( self == NSDate.class ) return [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: [object doubleValue]];
}
if ( [object isKindOfClass: NSDate.class] )
{
if ( self == NSNumber.class ) return [[NSNumber alloc] initWithDouble: [object timeIntervalSinceReferenceDate]];
if ( self == NSDecimalNumber.class ) return [[NSDecimalNumber alloc] initWithDouble: [object timeIntervalSinceReferenceDate]];
if ( self == NSDate.class ) return object;
}
if ( [object isKindOfClass: NSNull.class] )
{
if ( self == NSString.class ) return @"nil";
if ( self == NSNumber.class ) return @0;
if ( self == NSDecimalNumber.class ) return NSDecimalNumber.zero;
if ( self == NSDate.class ) return [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: 0];
if ( self == NSNull.class ) return object;
if ( [self isSubclassOfClass: NSArray.class] ) return @[];
if ( [self isSubclassOfClass: NSDictionary.class] ) return @{};
if ( [self isSubclassOfClass: NSSet.class] ) return [NSSet new];
return object;
}
if ( [object isKindOfClass: NSArray.class] )
{
if ( self == NSString.class ) return [((NSArray*) object) componentsJoinedByString: @"\n"];
if ( [self isSubclassOfClass: NSArray.class] ) return object;
if ( [self isSubclassOfClass: NSSet.class] ) return [[NSSet alloc] initWithArray: ((NSArray*) object)];
}
if ( [object isKindOfClass: NSSet.class] )
{
if ( self == NSString.class ) return [((NSSet*) object).allObjects componentsJoinedByString: @"\n"];
if ( [self isSubclassOfClass: NSArray.class] ) return ((NSSet*) object).allObjects;
if ( [self isSubclassOfClass: NSSet.class] ) return object;
}
if ( [object isKindOfClass: NSDictionary.class] )
{
if ( [self isSubclassOfClass: NSDictionary.class] ) return object;
}
return nil;
}
- (id) convertTo: (Class) type
{
if ( type == nil ) return self;
if ( [self isKindOfClass: type] ) return self;
id idself = self;
if ( [self isKindOfClass: NSString.class] )
{
if ( type == NSString.class ) return self;
if ( type == NSNumber.class ) return [[NSDecimalNumber alloc] initWithString: idself];
if ( type == NSDecimalNumber.class ) return [[NSDecimalNumber alloc] initWithString: idself];
if ( type == NSDate.class )
{
NSDate* date = [idself dateJSON];
if ( date == nil ) date = [idself dateRFC1123];
return date;
}
}
if ( [self isKindOfClass: NSNumber.class] )
{
if ( type == NSString.class ) return [idself stringValue];
if ( type == NSNumber.class ) return self;
if ( type == NSDecimalNumber.class ) return [[NSDecimalNumber alloc] initWithString: [idself stringValue]];
if ( type == NSDate.class ) return [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: [idself doubleValue]];
}
if ( [self isKindOfClass: NSDate.class] )
{
if ( type == NSNumber.class ) return [[NSNumber alloc] initWithDouble: [idself timeIntervalSinceReferenceDate]];
if ( type == NSDecimalNumber.class ) return [[NSDecimalNumber alloc] initWithDouble: [idself timeIntervalSinceReferenceDate]];
if ( type == NSDate.class ) return self;
}
if ( [self isKindOfClass: NSNull.class] )
{
if ( type == NSString.class ) return @"nil";
if ( type == NSNumber.class ) return @0;
if ( type == NSDecimalNumber.class ) return NSDecimalNumber.zero;
if ( type == NSDate.class ) return [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: 0];
if ( type == NSNull.class ) return self;
if ( [type isSubclassOfClass: NSArray.class] ) return @[];
if ( [type isSubclassOfClass: NSDictionary.class] ) return @{};
if ( [type isSubclassOfClass: NSSet.class] ) return [NSSet new];
return self;
}
if ( [self isKindOfClass: NSArray.class] )
{
if ( type == NSString.class ) return [((NSArray*) self) componentsJoinedByString: @"\n"];
if ( [type isSubclassOfClass: NSArray.class] ) return self;
if ( [type isSubclassOfClass: NSSet.class] ) return [[NSSet alloc] initWithArray: ((NSArray*) self)];
}
if ( [self isKindOfClass: NSSet.class] )
{
if ( type == NSString.class ) return [((NSSet*) self).allObjects componentsJoinedByString: @"\n"];
if ( [type isSubclassOfClass: NSArray.class] ) return ((NSSet*) self).allObjects;
if ( [type isSubclassOfClass: NSSet.class] ) return self;
}
if ( [self isKindOfClass: NSDictionary.class] )
{
if ( [type isSubclassOfClass: NSDictionary.class] ) return self;
}
return nil;
}
@end