nsarray+addition

.h文件

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
@interface (SafeAccess)
-(id)objectWithIndex:(NSUInteger)index;
- (NSString*)stringWithIndex:(NSUInteger)index;
- (NSNumber*)numberWithIndex:(NSUInteger)index;
- (NSArray*)arrayWithIndex:(NSUInteger)index;
- (NSDictionary*)dictionaryWithIndex:(NSUInteger)index;
- (NSInteger)integerWithIndex:(NSUInteger)index;
- (BOOL)boolWithIndex:(NSUInteger)index;
- (float)floatWithIndex:(NSUInteger)index;
- (CGFloat)CGFloatWithIndex:(NSUInteger)index;
- (CGPoint)pointWithIndex:(NSUInteger)index;
- (CGSize)sizeWithIndex:(NSUInteger)index;
- (CGRect)rectWithIndex:(NSUInteger)index;
@end
@interface NSMutableArray(SafeAccess)
-(void)addObj:(id)i;
-(void)addString:(NSString*)i;
-(void)addInteger:(NSInteger)i;
-(void)addCGFloat:(CGFloat)f;
-(void)addFloat:(float)i;
-(void)addPoint:(CGPoint)o;
-(void)addSize:(CGSize)o;
-(void)addRect:(CGRect)o;
@end

.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
@implementation (SafeAccess)
-(id)objectWithIndex:(NSUInteger)index{
if (index <self.count) {
return self[index];
}else{
return nil;
}
}
- (NSString*)stringWithIndex:(NSUInteger)index
{
id value = [self objectWithIndex:index];
if (value == nil || value == [NSNull null])
{
return @"";
}
if ([value isKindOfClass:[NSString class]]) {
return (NSString*)value;
}
if ([value isKindOfClass:[NSNumber class]]) {
return [value stringValue];
}
return nil;
}
- (NSNumber*)numberWithIndex:(NSUInteger)index
{
id value = [self objectWithIndex:index];
if ([value isKindOfClass:[NSNumber class]]) {
return (NSNumber*)value;
}
if ([value isKindOfClass:[NSString class]]) {
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
return [f numberFromString:(NSString*)value];
}
return nil;
}
- (NSArray*)arrayWithIndex:(NSUInteger)index
{
id value = [self objectWithIndex:index];
if (value == nil || value == [NSNull null])
{
return nil;
}
if ([value isKindOfClass:[NSArray class]])
{
return value;
}
return nil;
}
- (NSDictionary*)dictionaryWithIndex:(NSUInteger)index
{
id value = [self objectWithIndex:index];
if (value == nil || value == [NSNull null])
{
return nil;
}
if ([value isKindOfClass:[NSDictionary class]])
{
return value;
}
return nil;
}
- (NSInteger)integerWithIndex:(NSUInteger)index
{
id value = [self objectWithIndex:index];
if (value == nil || value == [NSNull null])
{
return 0;
}
if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]])
{
return [value integerValue];
}
return 0;
}
- (float)floatWithIndex:(NSUInteger)index
{
id value = [self objectWithIndex:index];
if (value == nil || value == [NSNull null])
{
return 0;
}
if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]])
{
return [value floatValue];
}
return 0;
}
- (CGFloat)CGFloatWithIndex:(NSUInteger)index
{
id value = [self objectWithIndex:index];
CGFloat f = [value doubleValue];
return f;
}
- (CGPoint)pointWithIndex:(NSUInteger)index
{
id value = [self objectWithIndex:index];
CGPoint point = CGPointFromString(value);
return point;
}
- (CGSize)sizeWithIndex:(NSUInteger)index
{
id value = [self objectWithIndex:index];
CGSize size = CGSizeFromString(value);
return size;
}
- (CGRect)rectWithIndex:(NSUInteger)index
{
id value = [self objectWithIndex:index];
CGRect rect = CGRectFromString(value);
return rect;
}
@end
@implementation NSMutableArray (SafeAccess)
-(void)addObj:(id)i{
if (i!=nil) {
[self addObject:i];
}
}
-(void)addString:(NSString*)i
{
if (i!=nil) {
[self addObject:i];
}
}
-(void)addBool:(BOOL)i
{
[self addObject:@(i)];
}
-(void)addInt:(int)i
{
[self addObject:@(i)];
}
-(void)addInteger:(NSInteger)i
{
[self addObject:@(i)];
}
-(void)addCGFloat:(CGFloat)f
{
[self addObject:@(f)];
}
-(void)addFloat:(float)i
{
[self addObject:@(i)];
}
-(void)addPoint:(CGPoint)o
{
[self addObject:NSStringFromCGPoint(o)];
}
-(void)addSize:(CGSize)o
{
[self addObject:NSStringFromCGSize(o)];
}
-(void)addRect:(CGRect)o
{
[self addObject:NSStringFromCGRect(o)];
}
@end

原文链接: http://blog.csdn.net/zhz459880251/article/details/50757445