@@ -7,9 +7,10 @@ The simplicity of the syntax compared to standard Core Data speaks for itself:
7
7
``` swift
8
8
let employees = moc.from (Employee).filter (" salary > 80000" ).all ()
9
9
let highestPaidEmployeeName = moc.from (Employee).order (descending : " salary" ).select (" name" ).first ()
10
- let highestSalary: NSNumber = moc.from (Employee).max (" salary" ).value ()!
10
+ let highestSalary = moc.from (Employee).max (" salary" ).value ()! as! NSNumber
11
11
let numberOfSmiths = moc.from (Employee).filter (" lastName = %@" , " Smith" ).count ()
12
12
13
+ // Iteration automatically causes the query to be executed.
13
14
for employee in moc.from (Employee).order (" startDate" ) {
14
15
debugPrintln (employee.firstName )
15
16
}
@@ -23,10 +24,10 @@ Core Data is an Objective-C API. Using it in Swift can be painful because of the
23
24
CDQI supports all of Core Data's result types: entities, dictionaries, ` NSManagedObjectID ` s, and (indirectly) counts:
24
25
25
26
``` swift
26
- let employees = moc.from (Employee).all ()
27
- let employeeFirstNames = moc.from (Employee).select (" firstName" ).all ()
28
- let employeeObjectIDs = moc.from (Employee).ids ().all ()
29
- let numberOfEmployees = moc.from (Employee).count ()
27
+ let employees = moc.from (Employee).all () // entities
28
+ let employeeFirstNames = moc.from (Employee).select (" firstName" ).all () // array of dictionaries
29
+ let employeeObjectIDs = moc.from (Employee).ids ().all () // object IDs
30
+ let numberOfEmployees = moc.from (Employee).count () // count
30
31
```
31
32
32
33
Queries are infinitely reusable. Later parts of a query have no side effects on earlier parts, e.g.,
@@ -50,6 +51,7 @@ To use such a query, you must specify an `NSManagedObjectContext` at the time th
50
51
employeeSalaryQuery.context (moc).all ()
51
52
employeeSalaryQuery.all (managedObjectContext : moc)
52
53
employeeSalaryQuery.first (managedObjectContext : moc)
54
+ // etc.
53
55
```
54
56
55
57
The ability to store and reuse queries without specifying a managed object context up front is one of the great advantages of CDQI.
0 commit comments