This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
forked from atom2ueki/LazyScrollView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTMUtils.m
183 lines (153 loc) · 3.86 KB
/
TMUtils.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
//
// TMUtils.m
// LazyScrollView
//
// Copyright (c) 2017 Alibaba. All rights reserved.
//
#import "TMUtils.h"
@implementation NSArray (TMUtil)
- (id)tm_safeObjectAtIndex:(NSUInteger)index
{
if (index >= [self count]) {
return nil;
}
id value = [self objectAtIndex:index];
if (value == [NSNull null]) {
return nil;
}
return value;
}
- (id)tm_safeObjectAtIndex:(NSUInteger)index class:(Class)aClass
{
id value = [self tm_safeObjectAtIndex:index];
if ([value isKindOfClass:aClass]) {
return value;
}
return nil;
}
- (bool)tm_boolAtIndex:(NSUInteger)index
{
id value = [self tm_safeObjectAtIndex:index];
if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) {
return [value boolValue];
}
return NO;
}
- (CGFloat)tm_floatAtIndex:(NSUInteger)index
{
id value = [self tm_safeObjectAtIndex:index];
if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) {
return [value floatValue];
}
return 0;
}
- (NSInteger)tm_integerAtIndex:(NSUInteger)index
{
id value = [self tm_safeObjectAtIndex:index];
if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) {
return [value integerValue];
}
return 0;
}
- (NSString *)tm_stringAtIndex:(NSUInteger)index
{
return [self tm_safeObjectAtIndex:index class:[NSString class]];
}
- (NSDictionary *)tm_dictionaryAtIndex:(NSUInteger)index
{
return [self tm_safeObjectAtIndex:index class:[NSDictionary class]];
}
- (NSArray *)tm_arrayAtIndex:(NSUInteger)index
{
return [self tm_safeObjectAtIndex:index class:[NSArray class]];
}
@end
@implementation NSMutableArray (TMUtil)
- (void)tm_safeAddObject:(id)anObject
{
if (anObject) {
[self addObject:anObject];
}
}
- (void)tm_safeInsertObject:(id)anObject atIndex:(NSUInteger)index
{
if (anObject && index <= self.count) {
[self insertObject:anObject atIndex:index];
}
}
@end
@implementation NSDictionary (TMUtil)
- (id)tm_safeObjectForKey:(id)key
{
if (key == nil) {
return nil;
}
id value = [self objectForKey:key];
if (value == [NSNull null]) {
return nil;
}
return value;
}
- (id)tm_safeObjectForKey:(id)key class:(Class)aClass
{
id value = [self tm_safeObjectForKey:key];
if ([value isKindOfClass:aClass]) {
return value;
}
return nil;
}
- (bool)tm_boolForKey:(id)key
{
id value = [self tm_safeObjectForKey:key];
if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) {
return [value boolValue];
}
return NO;
}
- (CGFloat)tm_floatForKey:(id)key
{
id value = [self tm_safeObjectForKey:key];
if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) {
return [value floatValue];
}
return 0;
}
- (NSInteger)tm_integerForKey:(id)key
{
id value = [self tm_safeObjectForKey:key];
if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) {
return [value integerValue];
}
return 0;
}
- (NSString *)tm_stringForKey:(id)key
{
return [self tm_safeObjectForKey:key class:[NSString class]];
}
- (NSDictionary *)tm_dictionaryForKey:(id)key
{
return [self tm_safeObjectForKey:key class:[NSDictionary class]];
}
- (NSArray *)tm_arrayForKey:(id)key
{
return [self tm_safeObjectForKey:key class:[NSArray class]];
}
- (id)tm_safeValueForKey:(NSString *)key
{
return [self tm_safeObjectForKey:key];
}
- (void)tm_safeSetValue:(id)value forKey:(NSString *)key
{
if (key && [key isKindOfClass:[NSString class]]) {
[self setValue:value forKey:key];
}
}
@end
@implementation NSMutableDictionary (TMUtil)
- (void)tm_safeSetObject:(id)anObject forKey:(id)key
{
if (key && anObject) {
[self setObject:anObject forKey:key];
}
}
@end