-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNSArray+TBL.m
138 lines (114 loc) · 3.76 KB
/
NSArray+TBL.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
#import "NSArray+TBL.h"
@implementation NSArray (TBL)
-(id)objectAtIndex:(NSUInteger)index kindOfClass:(Class)aClass {
id object = [self objectAtIndex:index];
if (![object isKindOfClass:aClass]) {
return nil;
}
return object;
}
-(BOOL)boolAtIndex:(NSUInteger)index defaultValue:(BOOL)defaultValue {
NSNumber *numValue = [self numberAtIndex:index];
if (numValue == nil) {
return defaultValue;
}
return [numValue boolValue];
}
-(NSNumber *)numberAtIndex:(NSUInteger)index {
return [self objectAtIndex:index kindOfClass:[NSNumber class]];
}
// returns a string or nil
-(NSString *)stringAtIndex:(NSUInteger)index {
return [self objectAtIndex:index kindOfClass:[NSString class]];
}
// returns a string or empty string, never nil
-(NSString *)stringOrEmptyStringAtIndex:(NSUInteger)index {
NSString *aString = [self stringAtIndex:index];
if (aString == nil) {
return @"";
}
return aString;
}
// returns a string or nil, or if the string is empty returns nil
-(NSString *)stringOrNilIfEmptyStringAtIndex:(NSUInteger)index {
NSString *aString = [self stringAtIndex:index];
if (aString != nil && [aString length] == 0) {
return nil;
}
return aString;
}
// converts the underlying object to a string or returns nil
-(NSString *)stringFromObjectAtIndex:(NSUInteger)index {
id object = [self objectAtIndex:index];
if (object == nil || [object isEqual:[NSNull null]]) {
return nil;
} else if ([object isKindOfClass:[NSString class]]) {
return object;
} else if ([object respondsToSelector:@selector(stringValue)]){
return [object stringValue];
}
return [object description];
}
// converts the underlying object to a string or empty string, never nil
-(NSString *)stringFromObjectOrEmptyStringAtIndex:(NSUInteger)index {
NSString *aString = [self stringFromObjectAtIndex:index];
if (aString == nil) {
return @"";
}
return aString;
}
-(NSArray *)arrayAtIndex:(NSUInteger)index {
return [self objectAtIndex:index kindOfClass:[NSArray class]];
}
-(NSDictionary *)dictionaryAtIndex:(NSUInteger)index {
return [self objectAtIndex:index kindOfClass:[NSDictionary class]];
}
// returns nil if index is outside of bounds
-(NSArray *)subarrayFromIndex:(NSUInteger)index {
if (index > [self count]) {
return nil;
}
return [self subarrayWithRange:NSMakeRange(index, [self count] - index)];
}
/**
Returns a subarray of the current array, up to the specified item (0-based index)
If the array has fewer than index+1 items, return the whole array
*/
-(NSArray *)subarrayToIndex:(NSUInteger)index {
if (index >= [self count]) {
return self;
}
return [self subarrayWithRange:NSMakeRange(0, index + 1)];
}
-(id)randomObject {
if ([self count] == 0) return nil;
uint32_t rnd = arc4random_uniform((uint32_t) [self count]);
return [self objectAtIndex:rnd];
}
#pragma mark -
#pragma mark Iteration
-(void)each:(void (^)(id thing))blockToExecute {
for (id thing in self) {
blockToExecute(thing);
}
}
-(void)eachKindOfClass:(Class)aClass block:(void (^)(id thing))blockToExecute {
for (id thing in self) {
if ([thing isKindOfClass:aClass]) {
blockToExecute(thing);
}
}
}
-(void)eachNumber:(void (^)(NSNumber *thing))blockToExecute {
[self eachKindOfClass:[NSNumber class] block:blockToExecute];
}
-(void)eachString:(void (^)(NSString *thing))blockToExecute {
[self eachKindOfClass:[NSString class] block:blockToExecute];
}
-(void)eachArray:(void (^)(NSArray *thing))blockToExecute {
[self eachKindOfClass:[NSArray class] block:blockToExecute];
}
-(void)eachDictionary:(void (^)(NSDictionary *thing))blockToExecute {
[self eachKindOfClass:[NSDictionary class] block:blockToExecute];
}
@end