forked from RestKit/RestKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRKSearchPredicate.m
More file actions
51 lines (39 loc) · 1.67 KB
/
Copy pathRKSearchPredicate.m
File metadata and controls
51 lines (39 loc) · 1.67 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
//
// RKSearchPredicate.m
// RestKit
//
// Created by Blake Watters on 7/27/12.
// Copyright (c) 2012 RestKit. All rights reserved.
//
#import "RKSearchPredicate.h"
#import "RKStringTokenizer.h"
@interface RKSearchPredicate()
- (instancetype)initWithType:(NSCompoundPredicateType)type subpredicates:(NSArray *)subpredicates NS_DESIGNATED_INITIALIZER;
@end
@implementation RKSearchPredicate
+ (NSPredicate *)searchPredicateWithText:(NSString *)searchText type:(NSCompoundPredicateType)type
{
return [[self alloc] initWithSearchText:searchText type:type];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:@"-initWithcoder: is not a valid initializer for the class %@, use designated initilizer -initWithSearchText:type:", NSStringFromClass([self class])]
userInfo:nil];
return [self init];
}
- (instancetype)initWithType:(NSCompoundPredicateType)type subpredicates:(NSArray *)subpredicates
{
return [super initWithType:type subpredicates:subpredicates];
}
- (instancetype)initWithSearchText:(NSString *)searchText type:(NSCompoundPredicateType)type
{
RKStringTokenizer *tokenizer = [RKStringTokenizer new];
NSSet *searchWords = [tokenizer tokenize:searchText];
NSMutableArray *subpredicates = [NSMutableArray arrayWithCapacity:[searchWords count]];
for (NSString *searchWord in searchWords) {
[subpredicates addObject:[NSPredicate predicateWithFormat:@"(ANY searchWords.word beginswith %@)", searchWord]];
}
return [super initWithType:type subpredicates:subpredicates];
}
@end