/
NSArray_BLTRExtensions.m
123 lines (104 loc) · 3.57 KB
/
NSArray_BLTRExtensions.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
//
// NSArray_Extensions.m
// Quicksilver
//
// Created by Alcor on Fri Apr 04 2003.
// Copyright (c) 2003 Blacktree, Inc. All rights reserved.
//
#import "NSArray_BLTRExtensions.h"
@implementation NSMutableArray (Moving)
- (void)moveIndex:(int)fromIndex toIndex:(int)toIndex {
if (fromIndex != toIndex){
[self insertObject:[self objectAtIndex:fromIndex] atIndex:toIndex];
if (toIndex<fromIndex)
fromIndex++;
[self removeObjectAtIndex:fromIndex];
}
}
- (void)insertObjectsFromArray:(NSArray *)array atIndex:(unsigned)index {
id object;
for(object in array)
[self insertObject:object atIndex:index];
}
@end
@implementation NSArray (IndexSet)
- (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes {
NSMutableArray *array = [NSMutableArray array];
unsigned int index;
for (index = [indexes firstIndex]; index != NSNotFound; index = [indexes indexGreaterThanIndex:index])
[array addObject:[self objectAtIndex:index]];
return array;
}
@end
@implementation NSArray (Transformation)
- (BOOL)hasPrefix:(NSArray *)prefixArray { return NO; }
- (NSString *)componentsJoinedByStrings:(NSArray *)strings {
return [[self arrayByPerformingSelector:@selector(componentsJoinedByStrings:) withObject:[strings tail]] componentsJoinedByString:[strings head]];
}
- (id)head { return [self count] ? [self objectAtIndex:0] : nil; }
- (NSArray *)tail { return [self count] > 1 ? [self subarrayWithRange:NSMakeRange(1, [self count]-1)] : nil; }
- (NSArray *)arrayByPerformingSelector:(SEL)aSelector {
NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:[self count]];
id result;
for (id anObject in self)
{
result = [anObject performSelector:aSelector];
[resultArray addObject:(result?result:[NSNull null])];
}
return resultArray;
}
- (NSArray *)arrayByPerformingSelector:(SEL)aSelector withObject:(id)object {
NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:[self count]];
int i;
id result = nil;
for (i = 0; i<(int) [self count]; i++) {
result = [[self objectAtIndex:i] performSelector:aSelector withObject:object];
[resultArray addObject:(result?result:[NSNull null])];
}
return resultArray;
}
- (id)objectWithValue:(id)value forKey:(NSString *)key {
for(id object in self) {
if ([[object valueForKey:key] isEqual:value])
return object;
}
return nil;
}
@end
@implementation NSObject (BLTRArrayPerform)
+ (NSMutableArray *)performSelector:(SEL)aSelector onObjectsInArray:(id)array returnValues:(BOOL)flag {
NSMutableArray *resultArray = nil;
if (flag)
resultArray = [NSMutableArray arrayWithCapacity:[array count]];
int i;
id result;
for (i = 0; i<(int) [array count]; i++) {
result = [self performSelector:aSelector withObject:[array objectAtIndex:i]];
if (flag)
[resultArray addObject:(result?result:[NSNull null])];
}
return resultArray;
}
- (NSMutableArray *)performSelector:(SEL)aSelector onObjectsInArray:(id)array returnValues:(BOOL)flag {
NSMutableArray *resultArray = nil;
if (flag)
resultArray = [NSMutableArray arrayWithCapacity:[array count]];
int i;
id result;
for (i = 0; i<(int) [array count]; i++) {
result = [self performSelector:aSelector withObject:[array objectAtIndex:i]];
if (flag) [resultArray addObject:(result?result:[NSNull null])];
}
return resultArray;
}
- (NSMutableArray *)performSelector:(SEL)aSelector onObjectsInArray:(id)array {
return [self performSelector:(SEL) aSelector onObjectsInArray:(id)array returnValues:YES];
}
@end
@implementation NSMutableArray (Reverse)
- (void)reverse {
int i;
for (i = 0; i < (floor([self count]/2.0) ); i++)
[self exchangeObjectAtIndex:i withObjectAtIndex:([self count]-i-1)];
}
@end