-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathNSMutableArray+Core.m
52 lines (39 loc) · 1.27 KB
/
NSMutableArray+Core.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
//
// NSMutableArray+core.m
//
// Created by Mike Laurence on 1/11/10.
// Copyright 2010 Mike Laurence. All rights reserved.
//
#import "NSMutableArray+Core.h"
#import "NSArray+Core.h"
@implementation NSMutableArray (Core)
#pragma mark -
#pragma mark Sorting
- (void) sortUsingKey:(id)key ascending:(BOOL)ascending {
[self sortUsingFunction:ascending ? ascendingSort : descendingSort context:key];
}
#pragma mark -
#pragma mark Padding
- (void) padToSize:(int)size {
[self padToSize:size withObject:[NSNull null]];
}
- (void) padToSize:(int)size withInstancesOfClass:(Class)klass {
for (int i = [self count]; i < size; i++) {
id obj = [[klass alloc] init];
if ([obj respondsToSelector:@selector(setIndex:)]) {
[obj performSelector:@selector(setIndex:) withObject:[NSNumber numberWithInt:i]];
}
[self addObject:obj];
}
}
- (void) padToSize:(int)size withObject:(id)obj {
for (int i = [self count]; i < size; i++)
[self addObject:obj];
}
- (void) padOrTruncateToSize:(int)size withInstancesOfClass:(Class)klass {
if (size > [self count])
[self padToSize:size withInstancesOfClass:klass];
else if (size < [self count])
[self removeObjectsInRange:NSMakeRange(size, [self count] - size)];
}
@end