-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuery.swift
65 lines (53 loc) · 2 KB
/
Query.swift
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
//
// Query.swift
// MyNotes
//
// Created by Luis Francisco Piura Mejia on 6/6/16.
// Copyright © 2016 Luis Francisco Piura Mejia. All rights reserved.
//
import UIKit
import CoreData
class Query: NSObject {
var className : String
private var request = NSFetchRequest()
init(className : String) {
self.className = className
}
func limit(n : Int) {
request.fetchLimit = n
}
func ascending(key : String) {
request.sortDescriptors = [NSSortDescriptor(key : key, ascending: true )]
}
func descending(key : String) {
request.sortDescriptors = [NSSortDescriptor(key : key, ascending: false )]
}
func equalTo(key : String, value : AnyObject) {
setTypeToPredicate(key, value: value, qoperator: "=")
}
func contains(key : String, substring : String) {
setTypeToPredicate(key, value: substring, qoperator: "CONTAINS[cd]")
}
func greaterThan(key : String, value : AnyObject) {
setTypeToPredicate(key, value: value, qoperator: ">")
}
func lessThan(key : String, value : AnyObject) {
setTypeToPredicate(key, value: value, qoperator: "<")
}
private func setTypeToPredicate (key : String, value : AnyObject, qoperator : String ) {
var afterPredicate = ""
if request.predicate != nil {
afterPredicate = "\(request.predicate!.predicateFormat) AND"
}
if let value = value as? String {
request.predicate = NSPredicate(format: "\(afterPredicate) \(key) \(qoperator) %@", value)
} else if let value = value as? NSNumber {
request.predicate = NSPredicate(format: "\(afterPredicate) \(key) \(qoperator) %@", value)
} else if let value = value as? NSDate {
request.predicate = NSPredicate(format: "\(afterPredicate) \(key) \(qoperator) %@", value)
}
}
func find() -> [AnyObject] {
return DataManager.getDataForClass(className, request: request)
}
}