NSArray+YOHO.m
1.47 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
//
// NSArray+YOHO.m
// YH_Mall
//
// Created by 张同海 on 15/5/29.
// Copyright (c) 2015年 YOHO. All rights reserved.
//
#import "NSArray+YOHO.h"
#import "NSMutableArray+YOHO.h"
@implementation NSArray (YOHO)
- (NSArray *)yh_arrayWithObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate
{
NSIndexSet *indexes = [self indexesOfObjectsPassingTest:predicate];
return [self objectsAtIndexes:indexes];
}
- (NSArray *)yh_arrayByRemovingObjectsOfClass:(Class)aClass
{
NSMutableArray *array = [NSMutableArray arrayWithArray:self];
[array yh_removeObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:aClass]) {
return YES;
}
else return NO;
}];
return [[NSArray alloc] initWithArray:array];
}
- (NSArray *)yh_arrayByKeepingObjectsOfClass:(Class)aClass
{
NSMutableArray *array = [NSMutableArray arrayWithArray:self];
[array yh_removeObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:aClass]) {
return NO;
}
else return YES;
}];
return [[NSArray alloc] initWithArray:array];
}
- (NSArray *)yh_arrayByRemovingObjectsFromArray:(NSArray *)otherArray
{
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return ![otherArray containsObject:evaluatedObject];
}];
return [self filteredArrayUsingPredicate:predicate];
}
@end