NSMutableArray+YOHO.m
1.5 KB
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
//
// NSMutableArray+YOHO.m
// YH_Mall
//
// Created by 张同海 on 15/5/29.
// Copyright (c) 2015年 YOHO. All rights reserved.
//
#import "NSMutableArray+YOHO.h"
@implementation NSMutableArray (YOHO)
+ (NSMutableArray *)yh_nullArrayWithCapacity:(NSUInteger)capacity
{
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:capacity];
for (NSInteger i = 0; i < capacity; i++) {
[array addObject:[NSNull null]];
}
return array;
}
- (void)yh_removeObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate
{
NSIndexSet *indexes = [self indexesOfObjectsPassingTest:predicate];
[self removeObjectsAtIndexes:indexes];
}
- (void)yh_removeLatterObjectsToKeepObjectsNoMoreThan:(NSInteger)maxCount
{
if ([self count] > maxCount) {
[self removeObjectsInRange:NSMakeRange(maxCount, [self count] - maxCount)];
}
}
- (void)yh_replaceObject:(id)anObject withObject:(id)anotherObject
{
NSInteger index = [self indexOfObject:anObject];
if (index != NSNotFound) {
[self replaceObjectAtIndex:index withObject:anotherObject];
}
}
- (void)yh_insertUniqueObject:(id)anObject
{
[self yh_insertUniqueObject:anObject atIndex:[self count]];
}
- (void)yh_insertUniqueObject:(id)anObject atIndex:(NSInteger)index
{
for (id object in self) {
if ([object isEqual:anObject]) {
return;
}
}
if (index < 0 || index > [self count]) {
return;
}
[self insertObject:anObject atIndex:index];
}
@end